OpenASIP  2.0
GlobalScope.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 GlobalScope.cc
26  *
27  * Implementation of GlobalScope class.
28  *
29  * @author Ari Metsähalme 2005 (ari.metsahalme-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include "GlobalScope.hh"
34 #include "CodeLabel.hh"
35 #include "DataLabel.hh"
36 #include "SequenceTools.hh"
37 #include "Program.hh"
38 #include "InstructionReference.hh"
40 
41 namespace TTAProgram {
42 
43 /////////////////////////////////////////////////////////////////////////////
44 // GlobalScope
45 /////////////////////////////////////////////////////////////////////////////
46 
47 /**
48  * Constructor.
49  */
51 }
52 
53 /**
54  * Destructor.
55  */
59 }
60 
61 /**
62  * Tells whether this is a global scope.
63  *
64  * @return True always.
65  */
66 bool
68  return true;
69 }
70 
71 /**
72  * Adds a code label to the global bookkeeping.
73  *
74  * @param codeLabel The label to be added.
75  * @param owner The owner scope of the label.
76  */
77 void
79  const CodeLabel& codeLabel, const Scope& owner) {
80 
81  allCodeLabels_.push_back(new LabelOwner(codeLabel, owner));
82 }
83 
84 /**
85  * Returns the number of code labels at the given address.
86  *
87  * Applies to all child scopes of this global scope.
88  *
89  * @param address The address of the label(s).
90  * @return The number of code labels at the given address.
91  */
92 int
94  int count = 0;
95  for (unsigned int i = 0; i < allCodeLabels_.size(); i++) {
96  if (allCodeLabels_.at(i)->label().address().location() ==
97  address.location() &&
98  &allCodeLabels_.at(i)->label().address().space() ==
99  &address.space()) {
100  count++;
101  }
102  }
103  return count;
104 }
105 
106 /**
107  * Returns the code label in the given address.
108  *
109  * Applies to all child scopes of this global scope.
110  *
111  * @param address The address of the label.
112  * @param index The index of the label if there are many labels at the
113  * address.
114  * @exception KeyNotFound If no code labels in the given address were found.
115  * @return The code label in the given address.
116  */
117 const CodeLabel&
118 GlobalScope::globalCodeLabel(Address address, int index) const {
119  int found = 0;
120 
121  for (unsigned int i = 0; i < allCodeLabels_.size(); i++) {
122 
123  if (allCodeLabels_.at(i)->label().address().location() ==
124  address.location() &&
125  &allCodeLabels_.at(i)->label().address().space() ==
126  &address.space()) {
127 
128  if (index == found) {
129  return dynamic_cast<const CodeLabel&>(
130  allCodeLabels_.at(i)->label());
131  } else {
132  found++;
133  }
134  }
135  }
136 
137  throw KeyNotFound(__FILE__, __LINE__);
138 }
139 
140 /**
141  * Returns the number of code labels in program.
142  *
143  * Applies to all child scopes of this global scope.
144  *
145  * @return The number of code labels in program.
146  */
147 int
149  return allCodeLabels_.size();
150 }
151 
152 /**
153  * Returns the code label of requested index.
154  *
155  * Applies to all child scopes of this global scope.
156  *
157  * @param index The index of the label.
158  * @exception KeyNotFound If no code labels in the given index.
159  * @return The code label in the given index.
160  */
161 const CodeLabel&
163  if (index < globalCodeLabelCount()) {
164  return dynamic_cast<const CodeLabel&>(
165  allCodeLabels_.at(index)->label());
166  }
167 
168  throw KeyNotFound(__FILE__, __LINE__);
169 }
170 
171 /**
172  * Adds a data label to the global bookkeeping.
173  *
174  * Applies to all child scopes of this global scope.
175  *
176  * @param dataLabel The label to be added.
177  * @param owner The owner scope of the label.
178  */
179 void
181  const DataLabel& dataLabel, const Scope& owner) {
182 
183  allDataLabels_.push_back(new LabelOwner(dataLabel, owner));
184 }
185 
186 /**
187  * Returns the number of data labels at the given address.
188  *
189  * Applies to all child scopes of this global scope.
190  *
191  * @param address The address of the label(s).
192  * @return The number of data labels at the given address.
193  */
194 int
196  int count = 0;
197  for (unsigned int i = 0; i < allDataLabels_.size(); i++) {
198  if (allDataLabels_.at(i)->label().address().location() ==
199  address.location() &&
200  &allDataLabels_.at(i)->label().address().space() ==
201  &address.space()) {
202  count++;
203  }
204  }
205  return count;
206 }
207 
208 /**
209  * Returns the data label in the given address.
210  *
211  * Applies to all child scopes of this global scope.
212  *
213  * @param address The address of the label.
214  * @param index The index of the label if there are many labels at the
215  * address.
216  * @exception KeyNotFound If no data labels in the given address were found.
217  * @return The data label in the given address.
218  */
219 const DataLabel&
220 GlobalScope::globalDataLabel(Address address, int index) const {
221  int found = 0;
222 
223  for (unsigned int i = 0; i < allDataLabels_.size(); i++) {
224 
225  if (allDataLabels_.at(i)->label().address().location() ==
226  address.location() &&
227  &allDataLabels_.at(i)->label().address().space() ==
228  &address.space()) {
229 
230  if (index == found) {
231  return dynamic_cast<const DataLabel&>(
232  allDataLabels_.at(i)->label());
233  } else {
234  found++;
235  }
236  }
237  }
238 
239  throw KeyNotFound(__FILE__, __LINE__);
240 }
241 
242 /**
243  * Returns the number of data labels in the program.
244  *
245  * Applies to all child scopes of this global scope.
246  *
247  * @return The number of data labels in the program.
248  */
249 int
251  return allDataLabels_.size();
252 }
253 
254 /**
255  * Returns the data label of given index.
256  *
257  * Applies to all child scopes of this global scope.
258  *
259  * @param index The index of the label.
260  * @exception KeyNotFound If no data labels with given index.
261  * @return The data label of given index.
262  */
263 const DataLabel&
265  if (index < globalDataLabelCount()) {
266  return dynamic_cast<const DataLabel&>(
267  allDataLabels_.at(index)->label());
268  }
269 
270  throw KeyNotFound(__FILE__, __LINE__);
271 }
272 
273 /**
274  * Creates a copy of the scope and its labels.
275  *
276  * The targets of the labels are not converted to point to the instructions of
277  * the given program and data of the data sections.
278  *
279  * @return A copy of the scope.
280  */
281 Scope*
283 
284  GlobalScope* newScope = new GlobalScope();
285  for (unsigned int i = 0; i < allDataLabels_.size(); ++i) {
286  const DataLabel& source = dynamic_cast<const DataLabel&>(
287  allDataLabels_.at(i)->label());
288  newScope->addDataLabel(new DataLabel(source));
289  }
290 
291  for (unsigned int i = 0; i < allCodeLabels_.size(); ++i) {
292  const CodeLabel& source = dynamic_cast<const CodeLabel&>(
293  allCodeLabels_.at(i)->label());
294  newScope->addCodeLabel(new CodeLabel(source));
295  }
296  return newScope;
297 }
298 
299 /**
300  * Sets the address space of all data labels.
301  *
302  * @param space The address space.
303  */
304 void
306  const TTAMachine::AddressSpace& space) {
307 
308  LabelOwnerList newDataLabels_;
309  for (int i = 0; i < globalDataLabelCount(); ++i) {
310  const DataLabel& source = globalDataLabel(i);
311 
312  LabelOwner* labelOwner = new LabelOwner(
313  *(new DataLabel(
314  source.name(), Address(source.address().location(), space),
315  *this)), *this);
316  newDataLabels_.push_back(labelOwner);
317  }
319  allDataLabels_ = newDataLabels_;
320 }
321 
322 /**
323  * Creates a copy of the scope and its labels.
324  *
325  * The targets of the labels are converted to point to the instructions of
326  * the given program. The data labels still point to the old address spaces,
327  * assuming the target machine is still the same.
328  *
329  * @param program The Program containing the instructions code labels should
330  * be converted to point to.
331  * @return A copy of the scope.
332  */
333 Scope*
335  const {
336 
337  GlobalScope* newScope = new GlobalScope();
338  for (unsigned int i = 0; i < allDataLabels_.size(); ++i) {
339  const Label& source = allDataLabels_.at(i)->label();
340  newScope->addDataLabel(new DataLabel(
341  source.name(), source.address(),
342  *newScope));
343  }
344 
345  for (unsigned int i = 0; i < allCodeLabels_.size(); ++i) {
346  const CodeLabel& source = dynamic_cast<const CodeLabel&>(
347  allCodeLabels_.at(i)->label());
348  newScope->addCodeLabel(
349  new CodeLabel(
350  program.instructionReferenceManager().createReference(
351  program.instructionAt(source.address().location())),
352  source.name()));
353  }
354  return newScope;
355 }
356 
357 /**
358  * Removes all code labels attached to the given instruction address.
359  *
360  * @param address The instruction address.
361  */
362 void
364 
365  for (LabelOwnerList::iterator i = allCodeLabels_.begin();
366  i != allCodeLabels_.end(); ) {
367  if ((*i)->label().address().location() == address) {
368  delete (*i);
369  i = allCodeLabels_.erase(i); //returns the following element or end
370  } else {
371  i++; // advance to next
372  }
373  }
374 }
375 
376 /////////////////////////////////////////////////////////////////////////////
377 // LabelOwner
378 /////////////////////////////////////////////////////////////////////////////
379 
380 /**
381  * Constructor.
382  *
383  * @param label The label.
384  * @param owner The owner scope of the label.
385  */
386 GlobalScope::LabelOwner::LabelOwner(const Label& label, const Scope& owner):
387  label_(&label), owner_(&owner) {
388 }
389 
390 /**
391  * Destructor.
392  *
393  * @note Is this really the owner of the label? Then it should delete it!
394  */
396 }
397 
398 /**
399  * Returns the label.
400  *
401  * @return The label.
402  */
403 const Label&
405  return *label_;
406 }
407 
408 /**
409  * Returns the owning scope of the label.
410  *
411  * @return The owning scope.
412  */
413 const Scope&
415  return *owner_;
416 }
417 
418 }
TTAProgram
Definition: Estimator.hh:65
TTAProgram::Program
Definition: Program.hh:63
TTAProgram::Scope::codeLabel
const CodeLabel & codeLabel(const std::string &name) const
Definition: Scope.cc:233
InstructionAddress
UInt32 InstructionAddress
Definition: BaseType.hh:175
TTAProgram::GlobalScope::LabelOwnerList
std::vector< LabelOwner * > LabelOwnerList
List for LabelOwners.
Definition: GlobalScope.hh:109
TTAProgram::Address
Definition: Address.hh:51
TTAMachine::AddressSpace
Definition: AddressSpace.hh:51
TTAProgram::Scope::addDataLabel
virtual void addDataLabel(const DataLabel *dataLabel)
Definition: Scope.cc:415
TTAProgram::GlobalScope::LabelOwner::label
const Label & label() const
Definition: GlobalScope.cc:404
SequenceTools.hh
TTAProgram::Label
Definition: Label.hh:55
TTAProgram::GlobalScope::copyAndRelocate
virtual Scope * copyAndRelocate(const TTAProgram::Program &program) const
Definition: GlobalScope.cc:334
TTAProgram::Address::space
const TTAMachine::AddressSpace & space() const
TTAProgram::Scope
Definition: Scope.hh:53
TTAProgram::GlobalScope::globalDataLabel
const DataLabel & globalDataLabel(Address address, int index) const
Definition: GlobalScope.cc:220
DataLabel.hh
TTAProgram::CodeLabel::address
virtual Address address() const
Definition: CodeLabel.cc:101
TTAProgram::GlobalScope::copy
virtual Scope * copy() const
Definition: GlobalScope.cc:282
TTAProgram::GlobalScope::allDataLabels_
LabelOwnerList allDataLabels_
Container for all data labels.
Definition: GlobalScope.hh:115
TTAProgram::GlobalScope::LabelOwner::~LabelOwner
~LabelOwner()
Definition: GlobalScope.cc:395
GlobalScope.hh
TTAProgram::GlobalScope::LabelOwner::scope
const Scope & scope() const
Definition: GlobalScope.cc:414
SequenceTools::deleteAllItems
static void deleteAllItems(SequenceType &aSequence)
TTAProgram::Label::name
std::string name() const
Definition: Label.cc:74
TTAProgram::GlobalScope::LabelOwner::LabelOwner
LabelOwner(const Label &label, const Scope &owner)
Definition: GlobalScope.cc:386
TTAProgram::Scope::addCodeLabel
virtual void addCodeLabel(const CodeLabel *codeLabel)
Definition: Scope.cc:376
TTAProgram::GlobalScope::setDataLabelAddressSpace
virtual void setDataLabelAddressSpace(const TTAMachine::AddressSpace &space)
Definition: GlobalScope.cc:305
TTAProgram::GlobalScope::globalDataLabelCount
int globalDataLabelCount() const
Definition: GlobalScope.cc:250
TTAProgram::DataLabel
Definition: DataLabel.hh:45
TTAProgram::GlobalScope::GlobalScope
GlobalScope()
Definition: GlobalScope.cc:50
TTAProgram::GlobalScope::allCodeLabels_
LabelOwnerList allCodeLabels_
Container for all code labels contained in the global scope and its child scopes.
Definition: GlobalScope.hh:113
TTAProgram::CodeLabel
Definition: CodeLabel.hh:49
TTAProgram::GlobalScope
Definition: GlobalScope.hh:47
TTAProgram::Address::location
InstructionAddress location() const
TTAProgram::Label::address
virtual Address address() const
Definition: Label.cc:84
TTAProgram::GlobalScope::removeCodeLabels
virtual void removeCodeLabels(InstructionAddress address)
Definition: GlobalScope.cc:363
TTAProgram::GlobalScope::~GlobalScope
virtual ~GlobalScope()
Definition: GlobalScope.cc:56
TTAProgram::GlobalScope::LabelOwner
Definition: GlobalScope.hh:90
Program.hh
InstructionReference.hh
InstructionReferenceManager.hh
TTAProgram::Scope::dataLabel
const DataLabel & dataLabel(const std::string &name) const
Definition: Scope.cc:251
TTAProgram::GlobalScope::globalCodeLabel
const CodeLabel & globalCodeLabel(Address address, int index) const
Definition: GlobalScope.cc:118
KeyNotFound
Definition: Exception.hh:285
program
find Finds info of the inner loops in the program
Definition: InnerLoopFinder.cc:80
TTAProgram::GlobalScope::addGlobalCodeLabel
virtual void addGlobalCodeLabel(const CodeLabel &codeLabel, const Scope &owner)
Definition: GlobalScope.cc:78
TTAProgram::GlobalScope::isGlobal
virtual bool isGlobal() const
Definition: GlobalScope.cc:67
TTAProgram::GlobalScope::addGlobalDataLabel
virtual void addGlobalDataLabel(const DataLabel &codeLabel, const Scope &owner)
Definition: GlobalScope.cc:180
CodeLabel.hh
TTAProgram::GlobalScope::globalCodeLabelCount
int globalCodeLabelCount() const
Definition: GlobalScope.cc:148