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

#include <CodeSection.hh>

Inheritance diagram for TPEF::CodeSection:
Inheritance graph
Collaboration diagram for TPEF::CodeSection:
Collaboration graph

Public Member Functions

virtual ~CodeSection ()
 
virtual void addElement (SectionElement *element)
 
virtual void setElement (Word index, SectionElement *element)
 
virtual SectionType type () const
 
virtual InstructionElementelement (Word index) const
 
void clearInstructionCache () const
 
Word instructionCount () const
 
InstructionElementinstruction (Word index) const
 
Word instructionToSectionIndex (Word index) const
 
Word indexOfElement (const InstructionElement &elem) const
 
Word indexOfInstruction (const InstructionElement &elem) const
 
bool isInSection (const InstructionElement &elem) const
 
bool isCodeSection () const override
 
- Public Member Functions inherited from TPEF::Section
virtual ~Section ()
 
virtual bool isChunkable () const
 
virtual Chunkchunk (SectionOffset offset) const
 
bool isProgramSection () const
 
bool isAuxSection () const
 
SectionElementelement (Word index) const
 
Word elementCount () const
 
void setFlagNoBits ()
 
void unsetFlagNoBits ()
 
bool noBits () const
 
bool vLen () const
 
Byte flags () const
 
void setFlags (Byte flagByte)
 
void setStartingAddress (AddressImage address)
 
AddressImage startingAddress () const
 
void setLink (const ReferenceManager::SafePointer *aLink)
 
void setLink (Section *aLink)
 
Sectionlink () const
 
void setASpace (const ReferenceManager::SafePointer *addrSpace)
 
void setASpace (ASpaceElement *addrSpace)
 
ASpaceElementaSpace () const
 
void setName (const ReferenceManager::SafePointer *sectionName)
 
void setName (Chunk *sectionName)
 
Chunkname () const
 
virtual bool isDataSection () const
 
- Public Member Functions inherited from TPEF::SafePointable
virtual ~SafePointable ()
 

Protected Member Functions

 CodeSection (bool init)
 
virtual Sectionclone () const
 
- Protected Member Functions inherited from TPEF::Section
 Section ()
 
void setFlagVLen ()
 
void unsetFlagVLen ()
 
- Protected Member Functions inherited from TPEF::SafePointable
 SafePointable ()
 

Private Member Functions

void initInstructionCache () const
 

Private Attributes

std::vector< Word > instructionStartCache_
 Cache vector of element indexes of those instruction elements, which begins a new instruction. More...
 
std::map< const InstructionElement *, Word > elementIndexCache_
 Cache of indexes of instruction elemenets. More...
 

Static Private Attributes

static CodeSection proto_
 Prototype instance of section. More...
 

Additional Inherited Members

- Public Types inherited from TPEF::Section
enum  SectionType {
  ST_NULL = 0x00, ST_STRTAB = 0x01, ST_SYMTAB = 0x02, ST_DEBUG = 0x03,
  ST_RELOC = 0x04, ST_LINENO = 0x05, ST_NOTE = 0x06, ST_ADDRSP = 0x07,
  ST_MR = 0x0A, ST_CODE = 0x81, ST_DATA = 0x82, ST_UDATA = 0x83,
  ST_LEDATA = 0x84, ST_DUMMY = 0xff
}
 
enum  SectionFlag { SF_VLEN = 0x40, SF_NOBITS = 0x80 }
 
- Static Public Member Functions inherited from TPEF::Section
static SectioncreateSection (SectionType type)
 
static bool isProgramSection (SectionType type)
 
- Static Protected Member Functions inherited from TPEF::Section
static void registerSection (const Section *section)
 

Detailed Description

Code section.

Definition at line 44 of file CodeSection.hh.

Constructor & Destructor Documentation

◆ ~CodeSection()

TPEF::CodeSection::~CodeSection ( )
virtual

Destructor.

Definition at line 58 of file CodeSection.cc.

58  {
59 }

◆ CodeSection()

TPEF::CodeSection::CodeSection ( bool  init)
protected

Constructor.

Parameters
initTrue if registeration is wanted.

Definition at line 46 of file CodeSection.cc.

46  :
47  Section() {
48  if (init) {
50  }
51  setFlagVLen();
53 }

References TPEF::Section::registerSection(), TPEF::Section::setFlagVLen(), and TPEF::Section::unsetFlagNoBits().

Referenced by clone().

Here is the call graph for this function:

Member Function Documentation

◆ addElement()

void TPEF::CodeSection::addElement ( SectionElement element)
virtual

Adds an element to section and clears internal caches.

Parameters
elementElement that is added to section.

Reimplemented from TPEF::Section.

Definition at line 267 of file CodeSection.cc.

267  {
270 }

References TPEF::Section::addElement(), clearInstructionCache(), and element().

Referenced by TTAProgram::ProgramWriter::createCodeSection(), CodeSectionCreator::finalize(), and TPEF::TPEFCodeSectionReader::readData().

Here is the call graph for this function:

◆ clearInstructionCache()

void TPEF::CodeSection::clearInstructionCache ( ) const

Clears instruction startpoint cache.

Definition at line 96 of file CodeSection.cc.

96  {
97  instructionStartCache_.clear();
98  elementIndexCache_.clear();
99 }

References elementIndexCache_, and instructionStartCache_.

Referenced by addElement(), and setElement().

◆ clone()

Section * TPEF::CodeSection::clone ( ) const
protectedvirtual

Creates an instance of CodeSection.

Returns
Newly created section.

Implements TPEF::Section.

Definition at line 77 of file CodeSection.cc.

77  {
78  return new CodeSection(false);
79 }

References CodeSection().

Here is the call graph for this function:

◆ element()

InstructionElement * TPEF::CodeSection::element ( Word  index) const
virtual

Returns requested instruction element.

Parameters
indexIndex of requested element.
Returns
Requested element.

Definition at line 88 of file CodeSection.cc.

88  {
89  return dynamic_cast<InstructionElement*>(Section::element(index));
90 }

References TPEF::Section::element().

Referenced by addElement(), TTAProgram::TPEFProgramFactory::addProcedures(), TPEFDisassembler::createInstruction(), TPEF::AOutSymbolSectionReader::finalize(), CodeCompressorPlugin::immediateTerminal(), TPEFDisassembler::initCache(), CodeCompressorPlugin::initializeRelocations(), initInstructionCache(), instruction(), and setElement().

Here is the call graph for this function:

◆ indexOfElement()

Word TPEF::CodeSection::indexOfElement ( const InstructionElement elem) const

Returns index of instruction element.

If section elements are changed, cache must be cleared before calling this function.

Parameters
elemElement whose index is returned.
Returns
index of instruction element.

Definition at line 198 of file CodeSection.cc.

198  {
200  return MapTools::valueForKey<Word>(elementIndexCache_, &elem);
201 
202 }

References elementIndexCache_, and initInstructionCache().

Referenced by TPEFDumper::section().

Here is the call graph for this function:

◆ indexOfInstruction()

Word TPEF::CodeSection::indexOfInstruction ( const InstructionElement elem) const

Returns index of instruction of requested element.

If requested element is in middle of instruction, then index of that instruction is returned.

If section elements are changed, cache must be cleared before calling this function.

Parameters
elemElement whose instruction index is returned.
Returns
index of instruction element.

Definition at line 232 of file CodeSection.cc.

232  {
234 
235  // get instruction element index
236  Word elementIndex =
237  MapTools::valueForKey<Word>(elementIndexCache_, &elem);
238 
239  Word instrCount = instructionCount();
240  int first = 0;
241  int last = instrCount - 1;
242  Word key = elementIndex;
243 
244  int middle = 0;
245 
246  // binary search from instructionStartCache for finding instruction index
247  while (first <= last) {
248  middle = (first + last) / 2;
249  if (key > instructionStartCache_[middle]) {
250  first = middle + 1;
251  } else if (key < instructionStartCache_[middle]) {
252  last = middle - 1;
253  } else {
254  break;
255  }
256  }
257 
258  return middle;
259 }

References elementIndexCache_, initInstructionCache(), instructionCount(), and instructionStartCache_.

Referenced by TPEFDumper::section(), and ProgramImageGenerator::writeDataSection().

Here is the call graph for this function:

◆ initInstructionCache()

void TPEF::CodeSection::initInstructionCache ( ) const
private

Initializes instruction cache.

Scans through section and marks element indexes of those instruction elements that starts a new instruction.

Also sets section element index for every element.

Definition at line 110 of file CodeSection.cc.

110  {
111 
112  // both caches must be initialized or uninitilized
114 
115  if (instructionStartCache_.empty()) {
116  for (Word i = 0; i < elementCount(); i++) {
117  InstructionElement* elem = element(i);
118 
119  // cache start index of instruction
120  if (elem->begin()) {
121  instructionStartCache_.push_back(i);
122  }
123 
124  // cache element index of instruction element
125  elementIndexCache_[elem] = i;
126 
127  }
128  }
129 
130  // there must be at least one instruction start
131  assert(!instructionStartCache_.empty());
132  assert(!elementIndexCache_.empty());
133 
134 }

References assert, TPEF::InstructionElement::begin(), element(), TPEF::Section::elementCount(), elementIndexCache_, and instructionStartCache_.

Referenced by indexOfElement(), indexOfInstruction(), instructionCount(), instructionToSectionIndex(), and isInSection().

Here is the call graph for this function:

◆ instruction()

InstructionElement & TPEF::CodeSection::instruction ( Word  index) const

Returns starting element of requested instruction.

See. instructionCount() for more information.

If section elements are changed, cache must be cleared before calling this function.

Parameters
indexIndex of requested instruction.
Returns
Starting element of requested instruction.

Definition at line 165 of file CodeSection.cc.

165  {
166  return *element(instructionToSectionIndex(index));
167 }

References element(), and instructionToSectionIndex().

Referenced by TTAProgram::ProgramWriter::createBinary(), TTAProgram::ProgramWriter::createRelocSections(), LabelManager::createSymbolWithReference(), TPEF::AOutSymbolSectionReader::finalize(), LabelManager::finalize(), and LabelManager::writeProcedureSymbols().

Here is the call graph for this function:

◆ instructionCount()

Word TPEF::CodeSection::instructionCount ( ) const

Returns number of instructions stored in section.

Each instruction elements that starts new instruction are marked with begin() flag.

If section elements are changed, cache must be cleared before calling this function.

Returns
Number of instructions in code section.

Definition at line 148 of file CodeSection.cc.

148  {
150  return instructionStartCache_.size();
151 }

References initInstructionCache(), and instructionStartCache_.

Referenced by TPEF::AOutSymbolSectionReader::finalize(), LabelManager::findRelocationDstSection(), TTAProgram::ProgramWriter::findSection(), LabelManager::findSectionByAddress(), indexOfInstruction(), and LabelManager::writeProcedureSymbols().

Here is the call graph for this function:

◆ instructionToSectionIndex()

Word TPEF::CodeSection::instructionToSectionIndex ( Word  index) const

Returns element index of starting element of requested instruction.

I.e. converts instruction index to instruction element index.

See. instructionCount() for more information.

If section elements are changed, cache must be cleared before calling this function.

Parameters
indexIndex of instruction
Returns
Instruction element index of requested instruction.

Definition at line 183 of file CodeSection.cc.

183  {
185  return instructionStartCache_[index];
186 }

References initInstructionCache(), and instructionStartCache_.

Referenced by TPEF::AOutSymbolSectionReader::finalize(), and instruction().

Here is the call graph for this function:

◆ isCodeSection()

bool TPEF::CodeSection::isCodeSection ( ) const
inlineoverridevirtual

Reimplemented from TPEF::Section.

Definition at line 66 of file CodeSection.hh.

66 { return true; }

◆ isInSection()

bool TPEF::CodeSection::isInSection ( const InstructionElement elem) const

Returns true if element is found from section.

If section elements are changed, cache must be cleared before calling this function.

Parameters
elemElement to check.
Returns
True if element is found from section.

Definition at line 214 of file CodeSection.cc.

214  {
217 }

References MapTools::containsKey(), elementIndexCache_, and initInstructionCache().

Referenced by TPEF::TPEFTools::sectionOfElement().

Here is the call graph for this function:

◆ setElement()

void TPEF::CodeSection::setElement ( Word  index,
SectionElement element 
)
virtual

Sets replaces an element with another and clears internal caches.

Parameters
indexIndex of element that is replaced.
elementElement that is set to given index.

Reimplemented from TPEF::Section.

Definition at line 279 of file CodeSection.cc.

279  {
282 }

References clearInstructionCache(), element(), and TPEF::Section::setElement().

Here is the call graph for this function:

◆ type()

Section::SectionType TPEF::CodeSection::type ( ) const
virtual

Returns section's type.

Returns
Type of section.

Implements TPEF::Section.

Definition at line 67 of file CodeSection.cc.

67  {
68  return ST_CODE;
69 }

References TPEF::Section::ST_CODE.

Member Data Documentation

◆ elementIndexCache_

std::map<const InstructionElement*, Word> TPEF::CodeSection::elementIndexCache_
mutableprivate

Cache of indexes of instruction elemenets.

Definition at line 81 of file CodeSection.hh.

Referenced by clearInstructionCache(), indexOfElement(), indexOfInstruction(), initInstructionCache(), and isInSection().

◆ instructionStartCache_

std::vector<Word> TPEF::CodeSection::instructionStartCache_
mutableprivate

Cache vector of element indexes of those instruction elements, which begins a new instruction.

Definition at line 78 of file CodeSection.hh.

Referenced by clearInstructionCache(), indexOfInstruction(), initInstructionCache(), instructionCount(), and instructionToSectionIndex().

◆ proto_

CodeSection TPEF::CodeSection::proto_
staticprivate

Prototype instance of section.

Definition at line 74 of file CodeSection.hh.


The documentation for this class was generated from the following files:
TPEF::CodeSection::element
virtual InstructionElement * element(Word index) const
Definition: CodeSection.cc:88
TPEF::CodeSection::initInstructionCache
void initInstructionCache() const
Definition: CodeSection.cc:110
TPEF::Section::setElement
virtual void setElement(Word index, SectionElement *element)
Definition: Section.cc:145
TPEF::Section::setFlagVLen
void setFlagVLen()
TPEF::CodeSection::instructionStartCache_
std::vector< Word > instructionStartCache_
Cache vector of element indexes of those instruction elements, which begins a new instruction.
Definition: CodeSection.hh:78
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::Section::element
SectionElement * element(Word index) const
TPEF::Section::addElement
virtual void addElement(SectionElement *element)
Definition: Section.cc:133
TPEF::Section::Section
Section()
Definition: Section.cc:64
TPEF::Section::unsetFlagNoBits
void unsetFlagNoBits()
TPEF::Section::registerSection
static void registerSection(const Section *section)
Definition: Section.cc:114
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
TPEF::CodeSection::instructionCount
Word instructionCount() const
Definition: CodeSection.cc:148
TPEF::CodeSection::instructionToSectionIndex
Word instructionToSectionIndex(Word index) const
Definition: CodeSection.cc:183
TPEF::Section::ST_CODE
@ ST_CODE
Text section.
Definition: Section.hh:79
TPEF::CodeSection::clearInstructionCache
void clearInstructionCache() const
Definition: CodeSection.cc:96
TPEF::CodeSection::elementIndexCache_
std::map< const InstructionElement *, Word > elementIndexCache_
Cache of indexes of instruction elemenets.
Definition: CodeSection.hh:81
TPEF::Section::elementCount
Word elementCount() const
TPEF::CodeSection::CodeSection
CodeSection(bool init)
Definition: CodeSection.cc:46