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

#include <TPEFCodeSectionReader.hh>

Inheritance diagram for TPEF::TPEFCodeSectionReader:
Inheritance graph
Collaboration diagram for TPEF::TPEFCodeSectionReader:
Collaboration graph

Protected Member Functions

 TPEFCodeSectionReader ()
 
virtual ~TPEFCodeSectionReader ()
 
virtual void readData (BinaryStream &stream, Section *section) const
 
virtual Section::SectionType type () const
 
virtual void readInfo (BinaryStream &stream, Section *sect) const
 
virtual HalfWord readId (BinaryStream &stream) const
 
- Protected Member Functions inherited from TPEF::TPEFSectionReader
 TPEFSectionReader ()
 
virtual BinaryReaderparent () const
 
- Protected Member Functions inherited from TPEF::SectionReader
 SectionReader ()
 
virtual void finalize (Section *section) const
 

Private Member Functions

 TPEFCodeSectionReader (const TPEFCodeSectionReader &)
 Copying not allowed. More...
 
TPEFCodeSectionReaderoperator= (TPEFCodeSectionReader &)
 Assignment not allowed. More...
 
void readAnnotations (BinaryStream &stream, InstructionElement *elem) const
 

Static Private Attributes

static TPEFCodeSectionReader proto_
 Prototype instance of TPEFCodeSectionReader to be registered to SectionReader. More...
 

Additional Inherited Members

- Public Types inherited from TPEF::SectionReader
typedef BinaryReader::Length Length
 
- Public Member Functions inherited from TPEF::TPEFSectionReader
virtual ~TPEFSectionReader ()
 
- Public Member Functions inherited from TPEF::SectionReader
virtual ~SectionReader ()
 
- Static Public Member Functions inherited from TPEF::SectionReader
static void readSection (BinaryStream &stream, Section *section, BinaryReader *reader)
 
static void finalizeBinary (Binary *binaryToFinalize, BinaryReader *reader)
 
- Static Protected Member Functions inherited from TPEF::TPEFSectionReader
static const Headerheader ()
 
- Static Protected Member Functions inherited from TPEF::SectionReader
static const SectionReaderfindSectionReader (const Section::SectionType type, const BinaryReader *bReader)
 
static void registerSectionReader (const SectionReader *sReader)
 

Detailed Description

Reads code section from TPEF binary file.

Definition at line 47 of file TPEFCodeSectionReader.hh.

Constructor & Destructor Documentation

◆ TPEFCodeSectionReader() [1/2]

TPEF::TPEFCodeSectionReader::TPEFCodeSectionReader ( )
protected

Constructor.

Registers itself to SectionReader.

Definition at line 60 of file TPEFCodeSectionReader.cc.

References TPEF::SectionReader::registerSectionReader().

Here is the call graph for this function:

◆ ~TPEFCodeSectionReader()

TPEF::TPEFCodeSectionReader::~TPEFCodeSectionReader ( )
protectedvirtual

Destructor.

Definition at line 67 of file TPEFCodeSectionReader.cc.

67  {
68 }

◆ TPEFCodeSectionReader() [2/2]

TPEF::TPEFCodeSectionReader::TPEFCodeSectionReader ( const TPEFCodeSectionReader )
private

Copying not allowed.

Member Function Documentation

◆ operator=()

TPEFCodeSectionReader& TPEF::TPEFCodeSectionReader::operator= ( TPEFCodeSectionReader )
private

Assignment not allowed.

◆ readAnnotations()

void TPEF::TPEFCodeSectionReader::readAnnotations ( BinaryStream stream,
InstructionElement elem 
) const
private

Reads annotation fields and adds them to instruction.

TODO: move to inline file...

Parameters
streamStream pointing to start of annotation.
elemInstruction where read annotations are added.

Definition at line 285 of file TPEFCodeSectionReader.cc.

286  {
287  bool continuation = true;
288 
289  while (continuation) {
290  Byte sizeAndContinuation = stream.readByte();
291 
292  // if continuation bit is down
293  if ((sizeAndContinuation & TPEFHeaders::IANNOTE_CONTINUATION) == 0) {
294  continuation = false;
295  }
296 
297 
298  Byte payloadSize = sizeAndContinuation & TPEFHeaders::IANNOTE_SIZE;
299 
300  // read three byte wide value (from big endian format)
301  Word id = stream.readByte() |
302  (static_cast<Word>(stream.readByte()) << (BYTE_BITWIDTH))|
303  (static_cast<Word>(stream.readByte()) << (BYTE_BITWIDTH*2));
304 
305  InstructionAnnotation *newAnnotation =
306  new InstructionAnnotation(id);
307 
308  for (int i = 0; i < payloadSize; i++) {
309  newAnnotation->addByte(stream.readByte());
310  }
311 
312  elem->addAnnotation(newAnnotation);
313  }
314 }

References TPEF::InstructionElement::addAnnotation(), TPEF::InstructionAnnotation::addByte(), BYTE_BITWIDTH, TPEF::TPEFHeaders::IANNOTE_CONTINUATION, TPEF::TPEFHeaders::IANNOTE_SIZE, and TPEF::BinaryStream::readByte().

Referenced by readData().

Here is the call graph for this function:

◆ readData()

void TPEF::TPEFCodeSectionReader::readData ( BinaryStream stream,
Section section 
) const
protectedvirtual

Reads section data from TPEF binary file.

Parameters
streamStream to be read from.
sectionSection where the information is to be stored.
Exceptions
UnreachableStreamIf reading of section fails.
KeyAlreadyExistsKey was in use when trying to register object.
EndOfFileIf end of file were reached while it shouldn't.
OutOfRangeSome of read values were out of range.
WrongSubclassSome class couldn't do what it was asked for.
UnexpectedValueIf there was unexpected value when reading.

Reimplemented from TPEF::TPEFSectionReader.

Definition at line 93 of file TPEFCodeSectionReader.cc.

93  {
94  // base classes implementation must be called with these.
95  TPEFSectionReader::readData(stream, section);
96 
97  CodeSection* codeSection = dynamic_cast<CodeSection*>(section);
98  assert(codeSection != NULL);
99 
100  bool nextIsBeginning = true;
101  Word sectionIndex = 0;
102 
103  // check that link section is defined properly
104  // (should point to machine resource section)
105  assert(header().linkId != 0);
106 
107  if (!section->noBits()) {
108 
109  while (stream.readPosition() <
110  header().bodyLength + header().bodyOffset) {
111 
112  SectionOffsetKey
113  sOffsetKey(header().sectionId,
114  stream.readPosition() - header().bodyOffset);
115 
116  Byte iAttr = stream.readByte();
117 
118  InstructionElement* newInstrElement = NULL;
119 
120  //if instruction is immediate
121  if (iAttr & TPEFHeaders::IA_TYPE) {
122 
123  ImmediateElement* newElem = new ImmediateElement();
124  newInstrElement = dynamic_cast<InstructionElement*>(newElem);
125 
126  newElem->setDestinationUnit(stream.readByte());
127  newElem->setDestinationIndex(stream.readByte());
128 
129  //iAttr mask 0xF0 number of bytes of immediate
130  Byte size = (iAttr >> (BYTE_BITWIDTH / 2));
131 
132  for (Byte i = 0; i < size; i++) {
133  newElem->addByte(stream.readByte());
134  }
135 
136  } else {
137 
138  MoveElement* newElem = new MoveElement();
139  newInstrElement = dynamic_cast<InstructionElement*>(newElem);
140 
141  newElem->setBus(readId(stream));
142 
143  Byte fieldTypes = stream.readByte();
144 
145  if (iAttr & TPEFHeaders::IA_EMPTY) {
146  newElem->setEmpty(true);
147 
148  } else {
149  newElem->setEmpty(false);
150 
151  switch (fieldTypes & TPEFHeaders::IE_SRC_TYPE_MASK) {
152  case TPEFHeaders::MVS_RF:
153  newElem->setSourceType(MoveElement::MF_RF);
154  break;
156  newElem->setSourceType(MoveElement::MF_IMM);
157  break;
159  newElem->setSourceType(MoveElement::MF_UNIT);
160  break;
161  default:
162  std::cerr << "field types: "
163  << std::hex << (int)fieldTypes
164  << std::dec << std::endl;
165  assert(false);
166  }
167 
168  switch (fieldTypes & TPEFHeaders::IE_DST_TYPE_MASK) {
169  case TPEFHeaders::MVD_RF:
170  newElem->setDestinationType(MoveElement::MF_RF);
171  break;
173  newElem->setDestinationType(MoveElement::MF_UNIT);
174  break;
175  default:
176  std::cerr << "field types: "
177  << std::hex << (int)fieldTypes
178  << std::dec << std::endl;
179  assert(false);
180  }
181 
182 
183  if (iAttr & TPEFHeaders::IA_MGUARD) {
184  newElem->setGuarded(true);
185 
186  switch (fieldTypes &
188 
190  newElem->setGuardType(MoveElement::MF_UNIT);
191  break;
192  case TPEFHeaders::MVG_RF:
193  newElem->setGuardType(MoveElement::MF_RF);
194  break;
195  default:
196  std::cerr << "field types: " << std::hex
197  << (int)fieldTypes
198  << std::dec << std::endl;
199  assert(false);
200  }
201 
202  } else {
203  newElem->setGuarded(false);
204  }
205  }
206 
207  newElem->setSourceUnit(readId(stream));
208  newElem->setSourceIndex(stream.readHalfWord());
209 
210  newElem->setDestinationUnit(readId(stream));
211  newElem->setDestinationIndex(stream.readHalfWord());
212 
213  newElem->setGuardUnit(readId(stream));
214  newElem->setGuardIndex(stream.readHalfWord());
215 
216  // guard extra parameters
217  if (fieldTypes & TPEFHeaders::IE_GUARD_INV_MASK) {
218  newElem->setGuardInverted(true);
219  } else {
220  newElem->setGuardInverted(false);
221  }
222  }
223 
224  newInstrElement->setBegin(nextIsBeginning);
225  nextIsBeginning = (iAttr & TPEFHeaders::IA_END);
226 
227  // create annotations for instruction if there are any
228  if (iAttr & TPEFHeaders::IA_ANNOTE) {
229  readAnnotations(stream, newInstrElement);
230  }
231 
232  // store reference to number of instruction for
233  // relocation destination resolving
234  if (newInstrElement->begin()) {
235  SectionIndexKey
236  sIndexKey(header().sectionId, sectionIndex);
237  SafePointer::addObjectReference(sIndexKey, newInstrElement);
238  sectionIndex++;
239  }
240 
241  SafePointer::addObjectReference(sOffsetKey, newInstrElement);
242  codeSection->addElement(newInstrElement);
243  }
244 
245  // add section size to parent
246  dynamic_cast<TPEFReader*>(
247  parent())->addSectionSize(section, sectionIndex);
248  }
249 }

References TPEF::ImmediateElement::addByte(), TPEF::CodeSection::addElement(), TPEF::ReferenceManager::SafePointer::addObjectReference(), assert, TPEF::InstructionElement::begin(), BYTE_BITWIDTH, TPEF::TPEFSectionReader::header(), TPEF::TPEFHeaders::IA_ANNOTE, TPEF::TPEFHeaders::IA_EMPTY, TPEF::TPEFHeaders::IA_END, TPEF::TPEFHeaders::IA_MGUARD, TPEF::TPEFHeaders::IA_TYPE, TPEF::TPEFHeaders::IE_DST_TYPE_MASK, TPEF::TPEFHeaders::IE_GUARD_INV_MASK, TPEF::TPEFHeaders::IE_GUARD_TYPE_MASK, TPEF::TPEFHeaders::IE_SRC_TYPE_MASK, TPEF::MoveElement::MF_IMM, TPEF::MoveElement::MF_RF, TPEF::MoveElement::MF_UNIT, TPEF::TPEFHeaders::MVD_RF, TPEF::TPEFHeaders::MVD_UNIT, TPEF::TPEFHeaders::MVG_RF, TPEF::TPEFHeaders::MVG_UNIT, TPEF::TPEFHeaders::MVS_IMM, TPEF::TPEFHeaders::MVS_RF, TPEF::TPEFHeaders::MVS_UNIT, TPEF::Section::noBits(), TPEF::TPEFSectionReader::parent(), readAnnotations(), TPEF::BinaryStream::readByte(), TPEF::TPEFSectionReader::readData(), TPEF::BinaryStream::readHalfWord(), readId(), TPEF::BinaryStream::readPosition(), TPEF::InstructionElement::setBegin(), TPEF::MoveElement::setBus(), TPEF::ImmediateElement::setDestinationIndex(), TPEF::MoveElement::setDestinationIndex(), TPEF::MoveElement::setDestinationType(), TPEF::ImmediateElement::setDestinationUnit(), TPEF::MoveElement::setDestinationUnit(), TPEF::MoveElement::setEmpty(), TPEF::MoveElement::setGuarded(), TPEF::MoveElement::setGuardIndex(), TPEF::MoveElement::setGuardInverted(), TPEF::MoveElement::setGuardType(), TPEF::MoveElement::setGuardUnit(), TPEF::MoveElement::setSourceIndex(), TPEF::MoveElement::setSourceType(), and TPEF::MoveElement::setSourceUnit().

Here is the call graph for this function:

◆ readId()

HalfWord TPEF::TPEFCodeSectionReader::readId ( BinaryStream stream) const
protectedvirtual

Reads Bus, FU or RF id according to TPEF version.

Original TPEF version 1 supports only less than 256 buses, FUs and RFs. Version 2 fixes that issue and we need to check the stream version for proper amount of bytes to read.

Parameters
streamStream to which the data is read.

Definition at line 326 of file TPEFCodeSectionReader.cc.

326  {
327 
328  TPEFHeaders::TPEFVersion version = stream.TPEFVersion();
329 
330  if (version == TPEFHeaders::TPEF_V1) {
331  return stream.readByte();
332  } else {
333  return stream.readHalfWord();
334  }
335 }

References TPEF::BinaryStream::readByte(), TPEF::BinaryStream::readHalfWord(), TPEF::TPEFHeaders::TPEF_V1, and TPEF::BinaryStream::TPEFVersion().

Referenced by readData().

Here is the call graph for this function:

◆ readInfo()

void TPEF::TPEFCodeSectionReader::readInfo ( BinaryStream stream,
Section sect 
) const
protectedvirtual

Reads the info field of code section header.

The ‘info’ field of code sections normally contains the size of instruction word in memory image (in MAUs) and the instruction encoding type identifier. This information is ignored by the TUT_TTA architecture. The read position of the stream is moved 4 bytes forward.

Todo:
Convert assertion onnstruction encoder identifier into a test with warning or error message.
Parameters
streamStream from which the field is read.
sectTarget section. Unused.

Reimplemented from TPEF::TPEFSectionReader.

Definition at line 266 of file TPEFCodeSectionReader.cc.

268  {
269 
270  stream.readHalfWord(); // ignored
271  assert(stream.readByte() == 0); // instruction encoding ID
272  stream.readByte(); // padding - should warn if nonzero
273 }

References assert, TPEF::BinaryStream::readByte(), and TPEF::BinaryStream::readHalfWord().

Here is the call graph for this function:

◆ type()

Section::SectionType TPEF::TPEFCodeSectionReader::type ( ) const
protectedvirtual

Returns the type of section it is meant to read.

Returns
The type of section it can read.

Implements TPEF::SectionReader.

Definition at line 76 of file TPEFCodeSectionReader.cc.

76  {
77  return Section::ST_CODE;
78 }

References TPEF::Section::ST_CODE.

Member Data Documentation

◆ proto_

TPEFCodeSectionReader TPEF::TPEFCodeSectionReader::proto_
staticprivate

Prototype instance of TPEFCodeSectionReader to be registered to SectionReader.

Definition at line 70 of file TPEFCodeSectionReader.hh.


The documentation for this class was generated from the following files:
TPEF::TPEFHeaders::IA_ANNOTE
@ IA_ANNOTE
Contains annotation.
Definition: TPEFHeaders.hh:123
TPEF::TPEFHeaders::IA_MGUARD
@ IA_MGUARD
Is conditional move or unconditional move.
Definition: TPEFHeaders.hh:126
TPEF::TPEFHeaders::IA_END
@ IA_END
Is end of instruction.
Definition: TPEFHeaders.hh:122
TPEF::TPEFHeaders::MVD_RF
@ MVD_RF
Destination is RF.
Definition: TPEFHeaders.hh:141
TPEF::TPEFSectionReader::TPEFSectionReader
TPEFSectionReader()
Definition: TPEFSectionReader.cc:50
TPEF::TPEFHeaders::MVS_RF
@ MVS_RF
Source is RF.
Definition: TPEFHeaders.hh:137
TPEF::MoveElement::MF_UNIT
@ MF_UNIT
Function unit.
Definition: MoveElement.hh:56
TPEF::TPEFHeaders::IANNOTE_CONTINUATION
@ IANNOTE_CONTINUATION
If there is more annotations.
Definition: TPEFHeaders.hh:113
TPEF::TPEFHeaders::IA_TYPE
@ IA_TYPE
Instruction type: move (0), immediate (1).
Definition: TPEFHeaders.hh:121
TPEF::MoveElement::MF_IMM
@ MF_IMM
Immediate.
Definition: MoveElement.hh:55
Byte
unsigned char Byte
Definition: BaseType.hh:116
TPEF::ReferenceManager::SafePointer::addObjectReference
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
Definition: SafePointer.cc:306
TPEF::MoveElement::MF_RF
@ MF_RF
Register file.
Definition: MoveElement.hh:54
TPEF::TPEFHeaders::MVS_UNIT
@ MVS_UNIT
Source is FU.
Definition: TPEFHeaders.hh:139
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::TPEFHeaders::TPEFVersion
TPEFVersion
Definition: TPEFHeaders.hh:56
TPEF::TPEFHeaders::MVG_RF
@ MVG_RF
Guard is RF.
Definition: TPEFHeaders.hh:145
TPEF::TPEFHeaders::IANNOTE_SIZE
@ IANNOTE_SIZE
Size of payload of annotation.
Definition: TPEFHeaders.hh:114
TPEF::TPEFHeaders::IA_EMPTY
@ IA_EMPTY
Empty instruction.
Definition: TPEFHeaders.hh:124
TPEF::TPEFHeaders::MVS_IMM
@ MVS_IMM
Source is immediate.
Definition: TPEFHeaders.hh:138
TPEF::TPEFCodeSectionReader::readId
virtual HalfWord readId(BinaryStream &stream) const
Definition: TPEFCodeSectionReader.cc:326
TPEF::TPEFHeaders::TPEF_V1
@ TPEF_V1
Initial TPEF version.
Definition: TPEFHeaders.hh:57
TPEF::SectionReader::registerSectionReader
static void registerSectionReader(const SectionReader *sReader)
Definition: SectionReader.cc:145
TPEF::TPEFHeaders::IE_GUARD_INV_MASK
@ IE_GUARD_INV_MASK
Guard inverted (1) means inverted.
Definition: TPEFHeaders.hh:146
TPEF::TPEFHeaders::IE_GUARD_TYPE_MASK
@ IE_GUARD_TYPE_MASK
If (1) guard points to GPR,(0) to FU.
Definition: TPEFHeaders.hh:135
TPEF::TPEFHeaders::MVG_UNIT
@ MVG_UNIT
Guard is FU.
Definition: TPEFHeaders.hh:144
TPEF::TPEFSectionReader::parent
virtual BinaryReader * parent() const
Definition: TPEFSectionReader.cc:65
TPEF::TPEFSectionReader::readData
virtual void readData(BinaryStream &stream, Section *section) const
Definition: TPEFSectionReader.cc:86
BYTE_BITWIDTH
const Byte BYTE_BITWIDTH
Definition: BaseType.hh:136
TPEF::TPEFHeaders::IE_DST_TYPE_MASK
@ IE_DST_TYPE_MASK
Instruction destination type mask.
Definition: TPEFHeaders.hh:134
TPEF::TPEFHeaders::MVD_UNIT
@ MVD_UNIT
Destination is FU.
Definition: TPEFHeaders.hh:143
TPEF::Section::ST_CODE
@ ST_CODE
Text section.
Definition: Section.hh:79
TPEF::TPEFCodeSectionReader::readAnnotations
void readAnnotations(BinaryStream &stream, InstructionElement *elem) const
Definition: TPEFCodeSectionReader.cc:285
TPEF::TPEFHeaders::IE_SRC_TYPE_MASK
@ IE_SRC_TYPE_MASK
Instruction source type mask.
Definition: TPEFHeaders.hh:133
TPEF::TPEFSectionReader::header
static const Header & header()
Definition: TPEFSectionReader.cc:174