OpenASIP  2.0
Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
TTAProgram::Scope Class Referenceabstract

#include <Scope.hh>

Inheritance diagram for TTAProgram::Scope:
Inheritance graph
Collaboration diagram for TTAProgram::Scope:
Collaboration graph

Public Member Functions

 Scope ()
 
virtual ~Scope ()
 
virtual bool isGlobal () const
 
virtual bool isUnit () const
 
virtual bool isProcedure () const
 
virtual bool isLocal () const
 
bool containsCodeLabel (const std::string &name) const
 
bool containsDataLabel (const std::string &name) const
 
const CodeLabelcodeLabel (const std::string &name) const
 
const DataLabeldataLabel (const std::string &name) const
 
int codeLabelCount (Address address) const
 
const CodeLabelcodeLabel (Address address, int index) const
 
int dataLabelCount (Address address) const
 
const DataLabeldataLabel (Address address, int index) const
 
virtual void addCodeLabel (const CodeLabel *codeLabel)
 
virtual void addDataLabel (const DataLabel *dataLabel)
 
virtual void removeCodeLabels (InstructionAddress address)
 
virtual Scopecopy () const =0
 

Protected Types

typedef std::vector< const Scope * > ScopeList
 List for child scopes. More...
 
typedef std::vector< const DataLabel * > DataLabelList
 List of data labels. More...
 
typedef std::vector< const CodeLabel * > CodeLabelList
 List of code labels. More...
 

Protected Member Functions

virtual void addGlobalCodeLabel (const CodeLabel &codeLabel, const Scope &owner)=0
 Adds a code label and its owner to the global label bookkeeping. More...
 
virtual void addGlobalDataLabel (const DataLabel &dataLabel, const Scope &owner)=0
 Adds a data label and its owner to the global label bookkeeping. More...
 
Scopeparent () const
 
void setParent (Scope &scope)
 
int childCount () const
 
void addChild (const Scope &scope)
 
const Scopechild (int index) const
 

Protected Attributes

ScopeList children_
 Child scopes. More...
 
DataLabelList dataLabels_
 Data labels contained by this scope. More...
 
CodeLabelList codeLabels_
 Code labels contained by this scope. More...
 

Private Member Functions

 Scope (const Scope &)
 Copying not allowed. More...
 
Scopeoperator= (const Scope &)
 Assignment not allowed. More...
 

Private Attributes

Scopeparent_
 The smallest outer scope that contains this scope. More...
 

Detailed Description

Scopes provide containers for different visibility level labels.

Todo:
Implement rest of the Scopes (only GlobalScope is supported currently).

Definition at line 53 of file Scope.hh.

Member Typedef Documentation

◆ CodeLabelList

typedef std::vector<const CodeLabel*> TTAProgram::Scope::CodeLabelList
protected

List of code labels.

Definition at line 90 of file Scope.hh.

◆ DataLabelList

typedef std::vector<const DataLabel*> TTAProgram::Scope::DataLabelList
protected

List of data labels.

Definition at line 88 of file Scope.hh.

◆ ScopeList

typedef std::vector<const Scope*> TTAProgram::Scope::ScopeList
protected

List for child scopes.

Definition at line 83 of file Scope.hh.

Constructor & Destructor Documentation

◆ Scope() [1/2]

Scope::Scope ( )

Constructor.

Definition at line 48 of file Scope.cc.

48  : parent_(NULL) {
49 }

◆ ~Scope()

Scope::~Scope ( )
virtual

Destructor.

Definition at line 54 of file Scope.cc.

54  {
55  for (unsigned int i = 0; i < dataLabels_.size(); i++) {
56  delete dataLabels_.at(i);
57  }
58  dataLabels_.clear();
59 
60  for (unsigned int i = 0; i < codeLabels_.size(); i++) {
61  delete codeLabels_.at(i);
62  }
63  codeLabels_.clear();
64 }

References codeLabels_, and dataLabels_.

◆ Scope() [2/2]

TTAProgram::Scope::Scope ( const Scope )
private

Copying not allowed.

Member Function Documentation

◆ addChild()

void Scope::addChild ( const Scope scope)
protected

Adds a child scope to this scope.

Parameters
scopeThe child scope to be added.
Exceptions
ObjectAlreadyExistsif the given scope has already been added. or a scope with the same name already exists.

Definition at line 114 of file Scope.cc.

114  {
116  children_.push_back(&scope);
117  } else {
118  throw ObjectAlreadyExists(__FILE__, __LINE__);
119  }
120 }

References children_, and ContainerTools::containsValue().

Here is the call graph for this function:

◆ addCodeLabel()

void Scope::addCodeLabel ( const CodeLabel codeLabel)
virtual

Adds a code label to this scope.

Parameters
codeLabelThe code label to be added.
Exceptions
KeyAlreadyExistsof a code label of the same name already exists.

Definition at line 376 of file Scope.cc.

376  {
377  if (containsCodeLabel(codeLabel->name())) {
378  throw KeyAlreadyExists(
379  __FILE__, __LINE__, "Scope::addCodeLabel()",
380  "A code label already exists by this name.");
381 
382  } else {
383  codeLabels_.push_back(codeLabel);
384 
385  // add label to the global bookkeeping
386  Scope* scope = NULL;
387 
388  if (!isGlobal()) {
389  scope = &parent();
390  while (!scope->isGlobal()) {
391  try {
392  scope = &scope->parent();
393  } catch (WrongSubclass) {
394  assert(false);
395  } catch (IllegalRegistration) {
396  assert(false);
397  }
398  }
399  } else {
400  scope = this;
401  }
402 
403  scope->addGlobalCodeLabel(*codeLabel, *this);
404  }
405 }

References addGlobalCodeLabel(), assert, codeLabel(), codeLabels_, containsCodeLabel(), isGlobal(), TTAProgram::Label::name(), and parent().

Referenced by TTAProgram::GlobalScope::copy(), TTAProgram::GlobalScope::copyAndRelocate(), TTAProgram::TPEFProgramFactory::createLabels(), and llvm::LLVMTCEBuilder::writeMachineFunction().

Here is the call graph for this function:

◆ addDataLabel()

void Scope::addDataLabel ( const DataLabel dataLabel)
virtual

Adds a data label to this scope.

Parameters
dataLabelThe data label to be added.
Exceptions
KeyAlreadyExistsof a data label of the same name already exists.

Definition at line 415 of file Scope.cc.

415  {
416  if (containsDataLabel(dataLabel->name())) {
417  throw KeyAlreadyExists(
418  __FILE__, __LINE__, "Scope::addDataLabel()",
419  "A data label already exists by this name.");
420  } else {
421  dataLabels_.push_back(dataLabel);
422 
423  // add label to the global bookkeeping
424  Scope* scope = NULL;
425 
426  if (!isGlobal()) {
427  scope = &parent();
428  while (!scope->isGlobal()) {
429  try {
430  scope = &scope->parent();
431  } catch (WrongSubclass) {
432  assert(false);
433  } catch (IllegalRegistration) {
434  assert(false);
435  }
436  }
437  } else {
438  scope = this;
439  }
440 
441  scope->addGlobalDataLabel(*dataLabel, *this);
442  }
443 }

References addGlobalDataLabel(), assert, containsDataLabel(), dataLabel(), dataLabels_, isGlobal(), TTAProgram::Label::name(), and parent().

Referenced by TTAProgram::GlobalScope::copy(), TTAProgram::GlobalScope::copyAndRelocate(), TTAProgram::TPEFProgramFactory::createLabels(), llvm::LLVMTCEBuilder::doFinalization(), and llvm::LLVMTCEBuilder::initDataSections().

Here is the call graph for this function:

◆ addGlobalCodeLabel()

virtual void TTAProgram::Scope::addGlobalCodeLabel ( const CodeLabel codeLabel,
const Scope owner 
)
protectedpure virtual

Adds a code label and its owner to the global label bookkeeping.

Parameters
dataLabelThe code label to be added.
ownerThe owner scope of the label.

Implemented in TTAProgram::GlobalScope.

Referenced by addCodeLabel().

◆ addGlobalDataLabel()

virtual void TTAProgram::Scope::addGlobalDataLabel ( const DataLabel dataLabel,
const Scope owner 
)
protectedpure virtual

Adds a data label and its owner to the global label bookkeeping.

Parameters
dataLabelThe data label to be added.
ownerThe owner scope of the label.

Implemented in TTAProgram::GlobalScope.

Referenced by addDataLabel().

◆ child()

const Scope & Scope::child ( int  index) const
protected

Returns the child scope at given index.

Parameters
indexThe index of the child scope.
Returns
The child scope at given index.
Exceptions
OutOfRangeif the exceeds the number of scopes contained in this scope.

Definition at line 184 of file Scope.cc.

184  {
185  if (index >= 0 && static_cast<unsigned int>(index) < children_.size()) {
186  return *children_.at(index);
187  } else {
188  throw OutOfRange(__FILE__, __LINE__);
189  }
190 }

References children_.

◆ childCount()

int Scope::childCount ( ) const
protected

Returns the number of contained scopes.

Only scopes directly contained in this scope are counted, not inner scopes inside one of the child scopes.

Returns
The number of contained scopes.

Definition at line 171 of file Scope.cc.

171  {
172  return children_.size();
173 }

References children_.

◆ codeLabel() [1/2]

const CodeLabel & Scope::codeLabel ( Address  address,
int  index 
) const

Returns a code label visible in this scope in the given address and index.

Parameters
addressThe address of the code label.
indexThe index of the label if there area many labels at this address.
Returns
A code label visible in this scope in the given address and index.

Definition at line 289 of file Scope.cc.

289  {
290  if (index < codeLabelCount(address)) {
291 
292  int found = -1;
293  for (unsigned int i = 0; i < codeLabels_.size(); i++) {
294 
295  if (&codeLabels_.at(i)->address().space() ==
296  &address.space() &&
297  codeLabels_.at(i)->address().location() ==
298  address.location()) {
299 
300  found++;
301  if (found == index) {
302  return *codeLabels_.at(i);
303  }
304  }
305  }
306 
307  throw KeyNotFound(__FILE__, __LINE__);
308 
309  } else {
310  throw OutOfRange(__FILE__, __LINE__);
311  }
312 }

References codeLabelCount(), codeLabels_, TTAProgram::Address::location(), and TTAProgram::Address::space().

Here is the call graph for this function:

◆ codeLabel() [2/2]

const CodeLabel & Scope::codeLabel ( const std::string &  name) const

Returns the code label with the given name.

Parameters
nameThe name of the label.
Exceptions
KeyNotFoundif no code label with the given name exists in this scope.
Returns
The code label with the given name.

Definition at line 233 of file Scope.cc.

233  {
234  for (unsigned int i = 0; i < codeLabels_.size(); i++) {
235  if (name == codeLabels_.at(i)->name()) {
236  return *codeLabels_.at(i);
237  }
238  }
239  throw KeyNotFound(__FILE__, __LINE__);
240 }

References codeLabels_.

Referenced by addCodeLabel(), TTAProgram::GlobalScope::addGlobalCodeLabel(), and SimControlLanguageCommand::parseInstructionAddressExpression().

◆ codeLabelCount()

int Scope::codeLabelCount ( Address  address) const

Returns the number of code labels in the given address visible in this scope.

Parameters
addressThe address of the label.
Returns
The number of code labels in the given address visible in this scope.

Definition at line 269 of file Scope.cc.

269  {
270  int count = 0;
271  for (unsigned int i = 0; i < codeLabels_.size(); i++) {
272  if (&codeLabels_.at(i)->address().space() == &address.space() &&
273  codeLabels_.at(i)->address().location() == address.location()) {
274  count++;
275  }
276  }
277  return count;
278 }

References codeLabels_, TTAProgram::Address::location(), and TTAProgram::Address::space().

Referenced by codeLabel().

Here is the call graph for this function:

◆ containsCodeLabel()

bool Scope::containsCodeLabel ( const std::string &  name) const

Tells whether this scope contains a code label with the given name.

Parameters
Thename of the label.
Returns
True if this scope contains a code label with the given name.

Definition at line 199 of file Scope.cc.

199  {
200  for (unsigned int i = 0; i < codeLabels_.size(); i++) {
201  if (name == codeLabels_.at(i)->name()) {
202  return true;
203  }
204  }
205  return false;
206 }

References codeLabels_.

Referenced by addCodeLabel().

◆ containsDataLabel()

bool Scope::containsDataLabel ( const std::string &  name) const

Tells whether this scope contains a data label with the given name.

Parameters
Thename of the label.
Returns
True if this scope contains a data label with the given name.

Definition at line 215 of file Scope.cc.

215  {
216  for (unsigned int i = 0; i < dataLabels_.size(); i++) {
217  if (name == dataLabels_.at(i)->name()) {
218  return true;
219  }
220  }
221  return false;
222 }

References dataLabels_.

Referenced by addDataLabel().

◆ copy()

virtual Scope* TTAProgram::Scope::copy ( ) const
pure virtual

Implemented in TTAProgram::GlobalScope.

◆ dataLabel() [1/2]

const DataLabel & Scope::dataLabel ( Address  address,
int  index 
) const

Returns a data label visible in this scope in the given address and index.

Parameters
addressThe address of the data label.
indexThe index of the label if there area many labels at this address.
Returns
A data label visible in this scope in the given address and index.

Definition at line 343 of file Scope.cc.

343  {
344  if (index < dataLabelCount(address)) {
345 
346  int found = -1;
347  for (unsigned int i = 0; i < dataLabels_.size(); i++) {
348 
349  if (&dataLabels_.at(i)->address().space() ==
350  &address.space() &&
351  dataLabels_.at(i)->address().location() ==
352  address.location()) {
353 
354  found++;
355  if (found == index) {
356  return *dataLabels_.at(i);
357  }
358  }
359  }
360 
361  throw KeyNotFound(__FILE__, __LINE__);
362 
363  } else {
364  throw OutOfRange(__FILE__, __LINE__);
365  }
366 }

References dataLabelCount(), dataLabels_, TTAProgram::Address::location(), and TTAProgram::Address::space().

Here is the call graph for this function:

◆ dataLabel() [2/2]

const DataLabel & Scope::dataLabel ( const std::string &  name) const

Returns the data label with the given name.

Parameters
nameThe name of the label.
Exceptions
KeyNotFoundif no data label with the given name exists in this scope.
Returns
The data label with the given name.

Definition at line 251 of file Scope.cc.

251  {
252  for (unsigned int i = 0; i < dataLabels_.size(); i++) {
253  if (name == dataLabels_.at(i)->name()) {
254  return *dataLabels_.at(i);
255  }
256  }
257  throw KeyNotFound(__FILE__, __LINE__);
258 }

References dataLabels_.

Referenced by addDataLabel(), TTAProgram::GlobalScope::addGlobalDataLabel(), SymbolAddressCommand::execute(), and SimControlLanguageCommand::parseDataAddressExpression().

◆ dataLabelCount()

int Scope::dataLabelCount ( Address  address) const

Returns the number of data labels in the given address visible in this scope.

Parameters
addressThe address of the label.
Returns
The number of data labels in the given address visible in this scope.

Definition at line 323 of file Scope.cc.

323  {
324  int count = 0;
325  for (unsigned int i = 0; i < dataLabels_.size(); i++) {
326  if (&dataLabels_.at(i)->address().space() == &address.space() &&
327  dataLabels_.at(i)->address().location() == address.location()) {
328  count++;
329  }
330  }
331  return count;
332 }

References dataLabels_, TTAProgram::Address::location(), and TTAProgram::Address::space().

Referenced by dataLabel().

Here is the call graph for this function:

◆ isGlobal()

bool Scope::isGlobal ( ) const
virtual

Tells whether this is a global scope.

Returns
True if this is a global scope.

Reimplemented in TTAProgram::GlobalScope.

Definition at line 72 of file Scope.cc.

72  {
73  return false;
74 }

Referenced by addCodeLabel(), addDataLabel(), parent(), and setParent().

◆ isLocal()

bool Scope::isLocal ( ) const
virtual

Tells whether this is a local scope.

Returns
True if this is a local scope.

Definition at line 102 of file Scope.cc.

102  {
103  return false;
104 }

◆ isProcedure()

bool Scope::isProcedure ( ) const
virtual

Tells whether this is a procedure scope.

Returns
True if this is a procedure scope.

Definition at line 92 of file Scope.cc.

92  {
93  return false;
94 }

◆ isUnit()

bool Scope::isUnit ( ) const
virtual

Tells whether this is a unit scope.

Returns
True if this is a unit scope.

Definition at line 82 of file Scope.cc.

82  {
83  return false;
84 }

◆ operator=()

Scope& TTAProgram::Scope::operator= ( const Scope )
private

Assignment not allowed.

◆ parent()

Scope & Scope::parent ( ) const
protected

Return the parent scope., that is, the scope that contains this scope.

Returns
The parent scope, that is, the scope that contains this scope.
Exceptions
WrongSubclassif this scope has no parent scope (it is the global scope).
IllegalRegistrationif no parent has been set (and the scope is not the global scope.

Definition at line 132 of file Scope.cc.

132  {
133  if (isGlobal()) {
134  throw WrongSubclass(
135  __FILE__, __LINE__, "Scope::parent()",
136  "Global scope cannot have parent scope.");
137  } else if (parent_ == NULL) {
138  throw IllegalRegistration(
139  __FILE__, __LINE__, "Scope::parent()", "No parent set.");
140  } else {
141  return *parent_;
142  }
143 }

References isGlobal(), and parent_.

Referenced by addCodeLabel(), and addDataLabel().

Here is the call graph for this function:

◆ removeCodeLabels()

void Scope::removeCodeLabels ( InstructionAddress  address)
virtual

Removes all code labels attached to the given instruction address.

Parameters
addressThe instruction address.

Reimplemented in TTAProgram::GlobalScope.

Definition at line 451 of file Scope.cc.

451  {
452 
453  for (CodeLabelList::iterator i = codeLabels_.begin();
454  i != codeLabels_.end(); ) {
455  CodeLabelList::iterator next = i;
456  next++;
457  if ((*i)->address().location() == address) {
458  delete (*i);
459  codeLabels_.erase(i);
460  }
461  i = next;
462  }
463 }

References codeLabels_.

◆ setParent()

void Scope::setParent ( Scope scope)
protected

Sets the parent scope.

Parameters
scopeThe new parent scope.
Exceptions
WrongSubclassif the scope is the global scope.

Definition at line 152 of file Scope.cc.

152  {
153  if (!isGlobal()) {
154  parent_ = &scope;
155  } else {
156  throw WrongSubclass(
157  __FILE__, __LINE__, "Scope::parent()",
158  "Global scope cannot have parent scope.");
159  }
160 }

References isGlobal(), and parent_.

Here is the call graph for this function:

Member Data Documentation

◆ children_

ScopeList TTAProgram::Scope::children_
protected

Child scopes.

Definition at line 85 of file Scope.hh.

Referenced by addChild(), child(), and childCount().

◆ codeLabels_

CodeLabelList TTAProgram::Scope::codeLabels_
protected

Code labels contained by this scope.

Definition at line 95 of file Scope.hh.

Referenced by addCodeLabel(), codeLabel(), codeLabelCount(), containsCodeLabel(), removeCodeLabels(), and ~Scope().

◆ dataLabels_

DataLabelList TTAProgram::Scope::dataLabels_
protected

Data labels contained by this scope.

Definition at line 93 of file Scope.hh.

Referenced by addDataLabel(), containsDataLabel(), dataLabel(), dataLabelCount(), and ~Scope().

◆ parent_

Scope* TTAProgram::Scope::parent_
private

The smallest outer scope that contains this scope.

Definition at line 125 of file Scope.hh.

Referenced by parent(), and setParent().


The documentation for this class was generated from the following files:
KeyAlreadyExists
Definition: Exception.hh:268
TTAProgram::Scope::codeLabel
const CodeLabel & codeLabel(const std::string &name) const
Definition: Scope.cc:233
TTAProgram::Scope::parent
Scope & parent() const
Definition: Scope.cc:132
TTAProgram::Scope::dataLabelCount
int dataLabelCount(Address address) const
Definition: Scope.cc:323
TTAProgram::Scope::codeLabels_
CodeLabelList codeLabels_
Code labels contained by this scope.
Definition: Scope.hh:95
TTAProgram::Scope::parent_
Scope * parent_
The smallest outer scope that contains this scope.
Definition: Scope.hh:125
OutOfRange
Definition: Exception.hh:320
TTAProgram::Scope::dataLabels_
DataLabelList dataLabels_
Data labels contained by this scope.
Definition: Scope.hh:93
Scope
Definition: MoveNode.hh:50
assert
#define assert(condition)
Definition: Application.hh:86
TTAProgram::Label::name
std::string name() const
Definition: Label.cc:74
WrongSubclass
Definition: Exception.hh:336
TTAProgram::Scope::isGlobal
virtual bool isGlobal() const
Definition: Scope.cc:72
IllegalRegistration
Definition: Exception.hh:532
ObjectAlreadyExists
Definition: Exception.hh:1002
TTAProgram::Scope::dataLabel
const DataLabel & dataLabel(const std::string &name) const
Definition: Scope.cc:251
TTAProgram::Scope::containsCodeLabel
bool containsCodeLabel(const std::string &name) const
Definition: Scope.cc:199
ContainerTools::containsValue
static bool containsValue(const ContainerType &aContainer, const ElementType &aKey)
KeyNotFound
Definition: Exception.hh:285
TTAProgram::Scope::codeLabelCount
int codeLabelCount(Address address) const
Definition: Scope.cc:269
TTAProgram::Scope::containsDataLabel
bool containsDataLabel(const std::string &name) const
Definition: Scope.cc:215
TTAProgram::Scope::children_
ScopeList children_
Child scopes.
Definition: Scope.hh:85