OpenASIP  2.0
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
TPEF::TPEFTools Class Reference

#include <TPEFTools.hh>

Collaboration diagram for TPEF::TPEFTools:
Collaboration graph

Public Member Functions

 TPEFTools (const Binary &aTpef)
 
void clearCache () const
 
SectionsectionOfElement (const SectionElement &element)
 
std::string resourceName (ResourceElement::ResourceType type, HalfWord resourceId) const
 
std::string addressSpaceName (const ASpaceElement &aSpace) const
 
bool hasRelocation (const SectionElement &element) const
 
const RelocElementrelocation (const SectionElement &element) const
 

Static Public Member Functions

static SectionsectionOfElement (const Binary &bin, const SectionElement &element)
 
static std::string resourceName (const Binary &bin, ResourceElement::ResourceType type, HalfWord resourceId)
 
static std::string addressSpaceName (const Binary &bin, const ASpaceElement &aSpace)
 
static bool hasRelocation (const Binary &bin, const SectionElement &element)
 
static const RelocElementrelocation (const Binary &bin, const SectionElement &element)
 

Private Member Functions

void initRelocationCache () const
 

Private Attributes

const Binarytpef_
 TPEF whose resources are accessed. More...
 
std::map< const SectionElement *, const RelocElement * > relocationCache_
 Cache of Chunks and immediates, which are relocated. More...
 

Detailed Description

Contains many helper functions that makes TPEF handling a bit easier.

These are kind of functions that doesn't fit to any Sections' or SectionElements' interfaces.

Class can be used stright through static interface, or client may create instance of TPEFTools, which enables caching functionality. Basically, if TPEFTools is highly utilized it should be used as a object.

Definition at line 62 of file TPEFTools.hh.

Constructor & Destructor Documentation

◆ TPEFTools()

TPEF::TPEFTools::TPEFTools ( const Binary aTpef)

Constructor.

Parameters
aTpefTPEF that is accessed with toolkit.

Definition at line 54 of file TPEFTools.cc.

54  : tpef_(aTpef) {
55 }

Member Function Documentation

◆ addressSpaceName() [1/2]

std::string TPEF::TPEFTools::addressSpaceName ( const ASpaceElement aSpace) const

Finds name string of an address space element.

Parameters
aSpaceElement whose name is needed.
Returns
Name of the requested address space.

Definition at line 96 of file TPEFTools.cc.

96  {
97  return addressSpaceName(tpef_, aSpace);
98 }

References tpef_.

Referenced by TTAProgram::TPEFProgramFactory::findAddressSpace().

◆ addressSpaceName() [2/2]

std::string TPEF::TPEFTools::addressSpaceName ( const Binary bin,
const ASpaceElement aSpace 
)
static

Finds name string of an address space element.

Parameters
binTPEF which is checked.
aSpaceElement whose name is needed.
Returns
Name of the requested address space.

Definition at line 246 of file TPEFTools.cc.

246  {
247 
248  assert(bin.sectionCount(Section::ST_ADDRSP) == 1);
249 
250  Section *aSpaceSection = bin.section(Section::ST_ADDRSP, 0);
251 
252  StringSection *strings = dynamic_cast<StringSection*>(
253  aSpaceSection->link());
254 
255  assert(strings != NULL);
256 
257  return strings->chunk2String(aSpace.name());
258 }

References assert, TPEF::StringSection::chunk2String(), TPEF::Section::link(), TPEF::ASpaceElement::name(), TPEF::Binary::section(), TPEF::Binary::sectionCount(), and TPEF::Section::ST_ADDRSP.

Here is the call graph for this function:

◆ clearCache()

void TPEF::TPEFTools::clearCache ( ) const

Clears cache of object.

Definition at line 61 of file TPEFTools.cc.

61  {
62  relocationCache_.clear();
63 }

References relocationCache_.

◆ hasRelocation() [1/2]

bool TPEF::TPEFTools::hasRelocation ( const Binary bin,
const SectionElement element 
)
static

Checks if the requested immediate or chunk is relocated.

Parameters
binTPEF which is checked.
elementElement whose name is needed.
Returns
True if the requested element has corresponding relocation element.

Definition at line 268 of file TPEFTools.cc.

268  {
269 
270  for (Word i = 0; i < bin.sectionCount(Section::ST_RELOC); i++) {
271  Section *relocSect = bin.section(Section::ST_RELOC, i);
272 
273  for (Word j = 0; j < relocSect->elementCount(); j++) {
274  RelocElement *reloc =
275  dynamic_cast<RelocElement*>(relocSect->element(j));
276 
277  assert(reloc != NULL);
278 
279  if (reloc->location() == &element) {
280  return true;
281  }
282  }
283  }
284 
285  return false;
286 }

References assert, TPEF::Section::element(), TPEF::Section::elementCount(), TPEF::RelocElement::location(), TPEF::Binary::section(), TPEF::Binary::sectionCount(), and TPEF::Section::ST_RELOC.

Here is the call graph for this function:

◆ hasRelocation() [2/2]

bool TPEF::TPEFTools::hasRelocation ( const SectionElement element) const

Checks if the requested immediate or chunk is relocated.

Parameters
elementElement whose name is needed.
Returns
True if the requested element has corresponding relocation element.

Definition at line 107 of file TPEFTools.cc.

107  {
109  return MapTools::containsKey(relocationCache_, &element);
110 }

References MapTools::containsKey(), initRelocationCache(), and relocationCache_.

Referenced by TTAProgram::TPEFProgramFactory::createInstruction(), and TTAProgram::TPEFProgramFactory::createTerminal().

Here is the call graph for this function:

◆ initRelocationCache()

void TPEF::TPEFTools::initRelocationCache ( ) const
private

Inits cache for hasRelocation and relocation methods.

Definition at line 323 of file TPEFTools.cc.

323  {
324 
325  if (relocationCache_.empty()) {
326  for (Word i = 0; i < tpef_.sectionCount(Section::ST_RELOC); i++) {
327  Section *relocSect = tpef_.section(Section::ST_RELOC, i);
328 
329  for (Word j = 0; j < relocSect->elementCount(); j++) {
330  RelocElement *reloc =
331  dynamic_cast<RelocElement*>(relocSect->element(j));
332 
333  assert(reloc != NULL);
334 
335  relocationCache_[reloc->location()] = reloc;
336  }
337  }
338  }
339 }

References assert, TPEF::Section::element(), TPEF::Section::elementCount(), TPEF::RelocElement::location(), relocationCache_, TPEF::Binary::section(), TPEF::Binary::sectionCount(), TPEF::Section::ST_RELOC, and tpef_.

Referenced by hasRelocation(), and relocation().

Here is the call graph for this function:

◆ relocation() [1/2]

const RelocElement & TPEF::TPEFTools::relocation ( const Binary bin,
const SectionElement element 
)
static

Retrieves the relocation element requested immediate or chunk is relocated.

Parameters
binTPEF which is checked.
elementElement relocations are checked.
Returns
Relocation element for the element given in parameter.

Definition at line 296 of file TPEFTools.cc.

296  {
297 
298  for (Word i = 0; i < bin.sectionCount(Section::ST_RELOC); i++) {
299  Section *relocSect = bin.section(Section::ST_RELOC, i);
300 
301  for (Word j = 0; j < relocSect->elementCount(); j++) {
302  RelocElement *reloc =
303  dynamic_cast<RelocElement*>(relocSect->element(j));
304 
305  assert(reloc != NULL);
306 
307  if (reloc->location() == &element) {
308  return *reloc;
309  }
310  }
311  }
312 
313  throw NotAvailable(
314  __FILE__, __LINE__, __func__,
315  "There is no relocation for requested element.");
316 }

References __func__, assert, TPEF::Section::element(), TPEF::Section::elementCount(), TPEF::RelocElement::location(), TPEF::Binary::section(), TPEF::Binary::sectionCount(), and TPEF::Section::ST_RELOC.

Here is the call graph for this function:

◆ relocation() [2/2]

const RelocElement & TPEF::TPEFTools::relocation ( const SectionElement element) const

Retrieves the relocation element requested immediate or chunk is relocated.

Parameters
elementElement relocations are checked.
Returns
Relocation element for the element given in parameter.

Definition at line 119 of file TPEFTools.cc.

119  {
121  return *MapTools::valueForKey<const RelocElement*>(
122  relocationCache_, &element);
123 }

References initRelocationCache(), and relocationCache_.

Referenced by TTAProgram::TPEFProgramFactory::createInstruction(), and TTAProgram::TPEFProgramFactory::createTerminal().

Here is the call graph for this function:

◆ resourceName() [1/2]

std::string TPEF::TPEFTools::resourceName ( const Binary bin,
ResourceElement::ResourceType  type,
HalfWord  resourceId 
)
static

Finds name string of a resource.

Parameters
binTPEF which is checked.
typeType of the resource.
resourceIdId of the resource.
Returns
Name of the requested resource.

Definition at line 208 of file TPEFTools.cc.

210  {
211 
212  for (Word i = 0; i < bin.sectionCount(Section::ST_MR); i++) {
213  ResourceSection* resources =
214  dynamic_cast<ResourceSection*>(bin.section(Section::ST_MR, i));
215 
216  assert(resources != NULL);
217 
218  if (resources->hasResource(type, resourceId)) {
219 
220  ResourceElement& resource =
221  dynamic_cast<ResourceElement&>(
222  resources->findResource(type, resourceId));
223 
224  StringSection *strings =
225  dynamic_cast<StringSection*>(resources->link());
226 
227  assert(strings != NULL);
228 
229  return strings->chunk2String(resource.name());
230  }
231  }
232 
233  throw NotAvailable(
234  __FILE__, __LINE__, __func__,
235  "Resource wasn't found id:" + Conversion::toString(resourceId));
236 }

References __func__, assert, TPEF::StringSection::chunk2String(), TPEF::ResourceSection::findResource(), TPEF::ResourceSection::hasResource(), TPEF::Section::link(), TPEF::ResourceElement::name(), TPEF::Binary::section(), TPEF::Binary::sectionCount(), TPEF::Section::ST_MR, and Conversion::toString().

Here is the call graph for this function:

◆ resourceName() [2/2]

std::string TPEF::TPEFTools::resourceName ( ResourceElement::ResourceType  type,
HalfWord  resourceId 
) const

Finds name string of a resource.

Parameters
typeType of the resource.
resourceIdId of the resource.
Returns
Name of the requested resource.

Definition at line 84 of file TPEFTools.cc.

85  {
86  return resourceName(tpef_, type, resourceId);
87 }

References tpef_.

◆ sectionOfElement() [1/2]

Section & TPEF::TPEFTools::sectionOfElement ( const Binary bin,
const SectionElement element 
)
static

Return section of the requested element.

NOTE: method does not work for LineNumElements (line number section element desing is wrong).

Parameters
binTPEF which is checked.
elementElement whose section is needed.
Returns
Section of the requested element.

Definition at line 136 of file TPEFTools.cc.

138  {
139 
140  // check if chunkable element
141  const Chunk* chunk = dynamic_cast<const Chunk*>(&element);
142 
143  if (chunk != NULL) {
144  for (Word i = 0; i < bin.sectionCount(); i++) {
145  RawSection* currSect =
146  dynamic_cast<RawSection*>(bin.section(i));
147 
148  if (currSect == NULL) {
149  continue;
150  }
151 
152  if (chunk->offset() < currSect->length() &&
153  currSect->chunk(chunk->offset()) == chunk) {
154  return *currSect;
155  }
156  }
157 
158  } else {
159  for (Word i = 0; i < bin.sectionCount(); i++) {
160  Section* currSect = bin.section(i);
161 
162  if (currSect->isChunkable()) {
163  continue;
164  }
165 
166  // optimization for code section
167  if (currSect->isCodeSection()) {
168  CodeSection* codeSect =
169  dynamic_cast<CodeSection*>(currSect);
170 
171  const InstructionElement* instr =
172  dynamic_cast<const InstructionElement*>(&element);
173 
174  if (instr == NULL) {
175  continue;
176  }
177 
178  if (codeSect->isInSection(*instr)) {
179  return *currSect;
180  } else {
181  continue;
182  }
183  }
184 
185  // other sections a bit slower
186  for (Word j = 0; j < currSect->elementCount(); j++) {
187  if (&element == currSect->element(j)) {
188  return *currSect;
189  }
190  }
191  }
192  }
193 
194  throw NotAvailable(
195  __FILE__, __LINE__, __func__,
196  "Requested element is not found from any section of binary.");
197 }

References __func__, TPEF::RawSection::chunk(), TPEF::Section::element(), TPEF::Section::elementCount(), TPEF::Section::isChunkable(), TPEF::Section::isCodeSection(), TPEF::CodeSection::isInSection(), TPEF::RawSection::length(), TPEF::Chunk::offset(), TPEF::Binary::section(), and TPEF::Binary::sectionCount().

Here is the call graph for this function:

◆ sectionOfElement() [2/2]

Section & TPEF::TPEFTools::sectionOfElement ( const SectionElement element)

Return section of the requested element.

Parameters
elementElement whose section is needed.
Returns
Section of the requested element.

Definition at line 72 of file TPEFTools.cc.

72  {
73  return sectionOfElement(tpef_, element);
74 }

References tpef_.

Member Data Documentation

◆ relocationCache_

std::map< const SectionElement*, const RelocElement*> TPEF::TPEFTools::relocationCache_
mutableprivate

Cache of Chunks and immediates, which are relocated.

Definition at line 110 of file TPEFTools.hh.

Referenced by clearCache(), hasRelocation(), initRelocationCache(), and relocation().

◆ tpef_

const Binary& TPEF::TPEFTools::tpef_
private

TPEF whose resources are accessed.

Definition at line 106 of file TPEFTools.hh.

Referenced by addressSpaceName(), initRelocationCache(), resourceName(), and sectionOfElement().


The documentation for this class was generated from the following files:
TPEF::TPEFTools::addressSpaceName
std::string addressSpaceName(const ASpaceElement &aSpace) const
Definition: TPEFTools.cc:96
TPEF::Binary::section
Section * section(Word index) const
TPEF::Binary::sectionCount
Word sectionCount() const
Conversion::toString
static std::string toString(const T &source)
NotAvailable
Definition: Exception.hh:728
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::TPEFTools::initRelocationCache
void initRelocationCache() const
Definition: TPEFTools.cc:323
__func__
#define __func__
Definition: Application.hh:67
TPEF::TPEFTools::resourceName
std::string resourceName(ResourceElement::ResourceType type, HalfWord resourceId) const
Definition: TPEFTools.cc:84
TPEF::TPEFTools::relocationCache_
std::map< const SectionElement *, const RelocElement * > relocationCache_
Cache of Chunks and immediates, which are relocated.
Definition: TPEFTools.hh:110
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
TPEF::TPEFTools::tpef_
const Binary & tpef_
TPEF whose resources are accessed.
Definition: TPEFTools.hh:106
TPEF::Section::ST_RELOC
@ ST_RELOC
Relocation section.
Definition: Section.hh:74
TPEF::TPEFTools::sectionOfElement
Section & sectionOfElement(const SectionElement &element)
Definition: TPEFTools.cc:72
TPEF::Section::ST_MR
@ ST_MR
Machine resources section.
Definition: Section.hh:78
TPEF::Section::ST_ADDRSP
@ ST_ADDRSP
Address space section.
Definition: Section.hh:77