OpenASIP  2.0
ConflictDetectionCodeGenerator.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 ConflictDetectionCodeGenerator.cc
26  *
27  * Definition of ConflictDetectionCodeGenerator class.
28  *
29  * @author Viljami Korhonen 2008 (viljami.korhonen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
35 #include "Application.hh"
36 #include "Machine.hh"
37 #include "FunctionUnit.hh"
38 #include "Operation.hh"
39 #include "Machine.hh"
40 #include "HWOperation.hh"
41 #include "Environment.hh"
42 
43 #include <string>
44 #include <sstream>
45 
46 using std::string;
47 using std::endl;
48 using namespace TTAMachine;
49 
50 /**
51  * The constructor.
52  *
53  * Gets the conflict detection settings and initializes the generator
54  * @param machine The machine model
55  *
56  */
59  const CompiledSimSymbolGenerator& symbolGenerator,
60  bool conflictDetectionEnabled) :
61  machine_(machine), conflictDetectionEnabled_(conflictDetectionEnabled),
62  symbolGen_(symbolGenerator) {
63 
65  return;
66  }
67 
68  string conflictDetectionSetting;
69 
70  std::string SETTING =
71  Environment::environmentVariable("TTASIM_CONFLICT_DETECTOR");
72  if (SETTING == "") {
73  conflictDetectionSetting = "AFSA"; // by default
74  } else {
75  conflictDetectionSetting = SETTING;
76  }
77 
78  if (conflictDetectionSetting == "CRT") {
79  conflictDetectorType_ = "ReservationTableFUResourceConflictDetector";
80  conflictDetectorMethod_ = "issueOperationInline";
81  conflictDetectorAdvanceCycle_ = "advanceCycle";
83  } else if (conflictDetectionSetting == "AFSA") {
84  conflictDetectorType_ = "FSAFUResourceConflictDetector";
85  conflictDetectorMethod_ = "issueOperationInline";
86  conflictDetectorAdvanceCycle_ = "advanceCycle";
87  conflictDetectorExtraInitMethod_ = "initializeAllStates";
88  } else if (conflictDetectionSetting == "LFSA") {
89  conflictDetectorType_ = "FSAFUResourceConflictDetector";
90  conflictDetectorMethod_ = "issueOperationLazyInline";
91  conflictDetectorAdvanceCycle_ = "advanceCycle";
93  } else if (conflictDetectionSetting == "DCM") {
94  conflictDetectorType_ = "DCMFUResourceConflictDetector";
95  conflictDetectorMethod_ = "issueOperationInline";
96  conflictDetectorAdvanceCycle_ = "advanceCycle";
98  } else {
100  << "Unknown CONFLICT_DETECTION_SETTING: '"
101  << conflictDetectionSetting << "'" << endl;
103  return;
104  }
105 
106  // initialize the detectors
108  << "Using conflict detector " << conflictDetectorType_ << std::endl;
109 
110  const Machine::FunctionUnitNavigator& fus =
112  for (int i = 0; i < fus.count(); ++i) {
113  const FunctionUnit& fu = *fus.item(i);
114  if (!fu.needsConflictDetection()) { // no need for conflict detection?
115  continue;
116  } else {
117  conflictDetectors_[fu.name()] =
119  }
120  }
121 
122  // No conflict detection is needed
123  if (conflictDetectors_.empty()) {
125  }
126 }
127 
128 /**
129  * The destructor
130  */
132 }
133 
134 /**
135  * Generates code for including the conflict detectors
136  * @return The generated code for including the conflict detectors
137  */
138 string
140 
142  return "";
143 
144  std::stringstream ss;
145  ss << "#include \"FSAFUResourceConflictDetector.hh\"" << endl << endl;
146  return ss.str();
147 }
148 
149 /**
150  * Creates symbol declaration code for the given FU's conflict detectors
151  *
152  * @param fu FU to create the conflict detector for
153  * @return The generated symbol declaration code
154  */
155 string
157 
159  return "";
160 
161  std::stringstream ss;
162  ss << "\t" << conflictDetectorType_ << " "
163  << symbolGen_.conflictDetectorSymbol(fu) << ";" << endl;
164 
165  return ss.str();
166 }
167 
168 /**
169  * Generates exra initialization call for conflict detectors that require a one
170  *
171  * @return The generated code for extra initialization call
172  */
173 string
175  std::stringstream ss;
177  for (ConflictDetectorObjectNameMap::const_iterator i =
178  conflictDetectors_.begin(); i != conflictDetectors_.end(); ++i) {
179  ss << "\t" << i->second << "."
180  << conflictDetectorExtraInitMethod_ << "();" << std::endl;
181  }
182  }
183  return ss.str();
184 }
185 
186 /**
187  * Generates code for notifying of detected conflicts
188  *
189  * @return Generated code for notifying of detected conflicts
190  */
191 string
193 
195  return "";
196 
197  std::stringstream ss;
198  ss << endl
199  << "\tif (conflictDetected_) {" << endl
200  << "\thaltSimulation(__FILE__, __LINE__, __FUNCTION__,"
201  << "\"Conflict detected!\");" << endl
202  << "\t}" << endl;
203 
204  return ss.str();
205 }
206 
207 /**
208  * Generates code for notifying of detected conflicts
209  *
210  * @return Generated code for notifying of detected conflicts
211  */
212 string
215  return "";
216 
217  std::stringstream ss;
218 
219  for (ConflictDetectorObjectNameMap::const_iterator i =
220  conflictDetectors_.begin(); i != conflictDetectors_.end(); ++i) {
221  ss << "\t," << i->second
222  << "(functionUnit(\"" << i->first << "\"))"
223  << endl;
224  }
225  return ss.str();
226 }
227 
228 /**
229  * Generates code for a function that advances clocks of each conflict detector
230  *
231  * @return A std::string containing generated code for the clock advances
232  */
233 string
235 
237  return "";
238 
239  std::stringstream ss;
240  // advance clock of all conflict detection models
241  for (ConflictDetectorObjectNameMap::const_iterator i =
242  conflictDetectors_.begin(); i != conflictDetectors_.end(); ++i) {
243  ss << "\t" << (*i).second << "."
244  << conflictDetectorAdvanceCycle_ << "();" << endl;
245  }
246 
247  return ss.str();
248 }
249 
250 /**
251  * Generates code for detecting an FU pipeline resource conflicts.
252  *
253  * @param op the triggered operation
254  * @return A std::string containing generated code for the operation call
255  */
256 string
258  const TTAMachine::HWOperation& op) {
259 
261  return "";
262 
263  const TTAMachine::FunctionUnit& fu = *op.parentUnit();
264 
265  if (conflictDetectors_.find(fu.name()) == conflictDetectors_.end())
266  return ""; // no conflict detection for the FU of the given operation
267 
268  int operationID = -1;
269  for (int i = 0; i < fu.operationCount(); ++i) {
270  if (fu.operation(i)->name() == op.name()) {
271  operationID = i;
272  break;
273  }
274  }
275  std::stringstream ss;
276  std::string detector = conflictDetectors_[fu.name()];
277  ss << "engine.conflictDetected_ |= !engine." << detector << "."
278  << conflictDetectorMethod_ << "(" << operationID << ");" << endl;
279  return ss.str();
280 }
281 
282 /**
283  * Returns a boolean describing if the conflict detection is enabled or not
284  *
285  * @return True if the conflict detection is enabled
286  */
287 bool
290 }
291 
ConflictDetectionCodeGenerator::conflictDetectorExtraInitMethod_
std::string conflictDetectorExtraInitMethod_
A method to be called on the conflict detector for initialization, empty if none.
Definition: ConflictDetectionCodeGenerator.hh:106
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
machine
TTAMachine::Machine * machine
the architecture definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:59
TTAMachine::HWOperation
Definition: HWOperation.hh:52
ConflictDetectionCodeGenerator::detectConflicts
std::string detectConflicts(const TTAMachine::HWOperation &op)
Definition: ConflictDetectionCodeGenerator.cc:257
ConflictDetectionCodeGenerator::conflictDetectors_
ConflictDetectorObjectNameMap conflictDetectors_
The resource conflict detector object name for each FU if any.
Definition: ConflictDetectionCodeGenerator.hh:111
ConflictDetectionCodeGenerator::symbolGen_
const CompiledSimSymbolGenerator & symbolGen_
Symbol generator.
Definition: ConflictDetectionCodeGenerator.hh:114
Application::logStream
static std::ostream & logStream()
Definition: Application.cc:155
ConflictDetectionCodeGenerator::conflictDetectionEnabled
bool conflictDetectionEnabled() const
Definition: ConflictDetectionCodeGenerator.cc:288
TTAMachine::Machine::Navigator::count
int count() const
ConflictDetectionCodeGenerator::conflictDetectorAdvanceCycle_
std::string conflictDetectorAdvanceCycle_
The method to be called on the conflict detector on cycle advance.
Definition: ConflictDetectionCodeGenerator.hh:103
ConflictDetectionCodeGenerator::includes
std::string includes()
Definition: ConflictDetectionCodeGenerator.cc:139
ConflictDetectionCodeGenerator::machine_
const TTAMachine::Machine & machine_
The machine.
Definition: ConflictDetectionCodeGenerator.hh:92
TTAMachine::FunctionUnit
Definition: FunctionUnit.hh:55
HWOperation.hh
ConflictDetectionCodeGenerator::~ConflictDetectionCodeGenerator
virtual ~ConflictDetectionCodeGenerator()
Definition: ConflictDetectionCodeGenerator.cc:131
TTAMachine::HWOperation::name
const std::string & name() const
Definition: HWOperation.cc:141
ConflictDetectionCodeGenerator::ConflictDetectionCodeGenerator
ConflictDetectionCodeGenerator(const TTAMachine::Machine &machine, const CompiledSimSymbolGenerator &, bool conflictDetectionEnabled=false)
Definition: ConflictDetectionCodeGenerator.cc:57
Application.hh
TTAMachine::Machine::functionUnitNavigator
virtual FunctionUnitNavigator functionUnitNavigator() const
Definition: Machine.cc:380
ConflictDetectionCodeGenerator::notifyOfConflicts
std::string notifyOfConflicts()
Definition: ConflictDetectionCodeGenerator.cc:192
TTAMachine::FunctionUnit::operationCount
virtual int operationCount() const
Definition: FunctionUnit.cc:419
Operation.hh
Environment.hh
Machine.hh
CompiledSimSymbolGenerator
Definition: CompiledSimSymbolGenerator.hh:66
ConflictDetectionCodeGenerator::conflictDetectorType_
std::string conflictDetectorType_
The FU resource conflict detector used, empty string if disabled.
Definition: ConflictDetectionCodeGenerator.hh:98
ConflictDetectionCodeGenerator::updateSymbolDeclarations
std::string updateSymbolDeclarations()
Definition: ConflictDetectionCodeGenerator.cc:213
TTAMachine::HWOperation::parentUnit
FunctionUnit * parentUnit() const
Definition: HWOperation.cc:190
TTAMachine::FunctionUnit::needsConflictDetection
bool needsConflictDetection() const
Definition: FunctionUnit.cc:804
ConflictDetectionCodeGenerator::symbolDeclaration
std::string symbolDeclaration(const TTAMachine::FunctionUnit &fu)
Definition: ConflictDetectionCodeGenerator.cc:156
TTAMachine::Machine::Navigator::item
ComponentType * item(int index) const
TTAMachine::FunctionUnit::operation
virtual HWOperation * operation(const std::string &name) const
Definition: FunctionUnit.cc:363
Environment::environmentVariable
static std::string environmentVariable(const std::string &variable)
Definition: Environment.cc:406
TTAMachine
Definition: Assembler.hh:48
ConflictDetectionCodeGenerator::conflictDetectorMethod_
std::string conflictDetectorMethod_
The method in the resource conflict detector used to detect conflicts, empty string if disabled.
Definition: ConflictDetectionCodeGenerator.hh:101
CompiledSimSymbolGenerator::conflictDetectorSymbol
std::string conflictDetectorSymbol(const TTAMachine::FunctionUnit &fu) const
Definition: CompiledSimSymbolGenerator.cc:270
ConflictDetectionCodeGenerator.hh
TTAMachine::Machine::Navigator
Definition: Machine.hh:186
ConflictDetectionCodeGenerator::advanceClockCode
std::string advanceClockCode()
Definition: ConflictDetectionCodeGenerator.cc:234
CompiledSimSymbolGenerator.hh
ConflictDetectionCodeGenerator::extraInitialization
std::string extraInitialization()
Definition: ConflictDetectionCodeGenerator.cc:174
TTAMachine::Machine
Definition: Machine.hh:73
FunctionUnit.hh
ConflictDetectionCodeGenerator::conflictDetectionEnabled_
bool conflictDetectionEnabled_
True, if the conflict detection is enabled.
Definition: ConflictDetectionCodeGenerator.hh:95