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

#include <TPEFWriter.hh>

Inheritance diagram for TPEF::TPEFWriter:
Inheritance graph
Collaboration diagram for TPEF::TPEFWriter:
Collaboration graph

Static Public Member Functions

static const BinaryWriterinstance ()
 

Protected Member Functions

 TPEFWriter ()
 
 ~TPEFWriter ()
 
virtual void actualWriteBinary (BinaryStream &stream, const Binary *bin) const
 
- Protected Member Functions inherited from TPEF::BinaryWriter
 BinaryWriter ()
 
void setWriter (const BinaryWriter *writer)
 

Static Private Attributes

static BinaryWriterinstance_ = NULL
 Instance which is used for writing binary file. More...
 

Additional Inherited Members

- Public Member Functions inherited from TPEF::BinaryWriter
virtual ~BinaryWriter ()
 
void writeBinary (BinaryStream &stream, const Binary *bin) const
 

Detailed Description

Class for writing TPEF binary files.

Definition at line 44 of file TPEFWriter.hh.

Constructor & Destructor Documentation

◆ TPEFWriter()

TPEF::TPEFWriter::TPEFWriter ( )
protected

Constructor.

Definition at line 55 of file TPEFWriter.cc.

55  : BinaryWriter() {
56  setWriter(this);
57 }

References TPEF::BinaryWriter::setWriter().

Referenced by instance().

Here is the call graph for this function:

◆ ~TPEFWriter()

TPEF::TPEFWriter::~TPEFWriter ( )
protected

Destructor.

Definition at line 62 of file TPEFWriter.cc.

62  {
63 }

Member Function Documentation

◆ actualWriteBinary()

void TPEF::TPEFWriter::actualWriteBinary ( BinaryStream stream,
const Binary bin 
) const
protectedvirtual

Writes binary object model in to a binary stream.

Parameters
streamStream where to write.
binBinary to write.

Implements TPEF::BinaryWriter.

Definition at line 84 of file TPEFWriter.cc.

86  {
87 
88  // check that there is only one resource section and null section
89  if (bin->sectionCount(Section::ST_ADDRSP) > 1 ||
90  bin->sectionCount(Section::ST_MR) > 1) {
91 
92  bool onlyOneAddressSpaceAndResourceSectionIsAllowed = false;
93  assert(onlyOneAddressSpaceAndResourceSectionIsAllowed);
94  }
95 
96  FileOffset startOffset = stream.writePosition();
97 
98  // file header must be written first
99  stream.setWritePosition(startOffset + TPEFHeaders::FH_ID);
100  for (Word i = 0; i < TPEFHeaders::FH_ID_SIZE; i++) {
101  // sets stream tpef version same as binary version
102  if (i == 8) {
103  TPEFHeaders::TPEFVersion version = bin->TPEFVersion();
104  stream.setTPEFVersion(version);
105  stream.writeByte(version);
106  } else {
107  stream.writeByte(TPEFHeaders::FH_ID_BYTES[i]);
108  }
109  }
110 
111  stream.setWritePosition(startOffset + TPEFHeaders::FH_ARCH);
112  stream.writeByte(bin->arch());
113 
114  stream.setWritePosition(startOffset + TPEFHeaders::FH_TYPE);
115  stream.writeByte(bin->type());
116 
117  // offset to the first section header
118  stream.setWritePosition(startOffset + TPEFHeaders::FH_SHOFF);
119  stream.writeWord(TPEFHeaders::FH_HEADER_SIZE);
120 
121  // size of file header
122  stream.setWritePosition(startOffset + TPEFHeaders::FH_SIZE);
123  stream.writeHalfWord(TPEFHeaders::FH_HEADER_SIZE);
124 
125  stream.setWritePosition(startOffset + TPEFHeaders::FH_SHSIZE);
126  stream.writeHalfWord(TPEFHeaders::SH_HEADER_SIZE);
127 
128  // number of sections
129  stream.setWritePosition(startOffset + TPEFHeaders::FH_SHNUM);
130  stream.writeHalfWord(bin->sectionCount());
131 
132  // file offset to the header of string table section
133  stream.setWritePosition(startOffset + TPEFHeaders::FH_SHSTRTAB);
134  if (bin->strings() != NULL) {
135  FileOffsetReplacer replacer(bin->strings());
136  replacer.resolve();
137  } else {
138  stream.writeWord(0);
139  }
140 
141  // offset of first header
142  FileOffset sectionHeaderOffset = TPEFHeaders::FH_HEADER_SIZE;
143 
144  Section* section = NULL;
145 
146  for (Word i = 0; i < bin->sectionCount(); i++) {
147  section = bin->section(i);
148  try {
149  stream.setWritePosition(startOffset + sectionHeaderOffset);
150  SectionWriter::writeHeader(stream, section, this);
151  sectionHeaderOffset += TPEFHeaders::SH_HEADER_SIZE;
152 
153  } catch (const InstanceNotFound& i) {
154  std::cerr << "Writer for a section header not found"
155  << std::endl;
156  continue;
157  }
158  }
159 
160  // then the section bodies
161  for (Word i = 0; i < bin->sectionCount(); i++) {
162  section = bin->section(i);
163  try {
164  if (!section->noBits()) {
165  SectionWriter::writeData(stream, section, this);
166  } else {
167  // TODO: If we are in this branch verify that there is
168  // only undef elements in section.
169  // i.e. verify that section is empty...
170  }
171  } catch(const InstanceNotFound& i) {
172  abortWithError("Writer for a section body not found");
173  }
174  }
175 }

References abortWithError, TPEF::Binary::arch(), assert, TPEF::TPEFHeaders::FH_ARCH, TPEF::TPEFHeaders::FH_HEADER_SIZE, TPEF::TPEFHeaders::FH_ID, TPEF::TPEFHeaders::FH_ID_BYTES, TPEF::TPEFHeaders::FH_ID_SIZE, TPEF::TPEFHeaders::FH_SHNUM, TPEF::TPEFHeaders::FH_SHOFF, TPEF::TPEFHeaders::FH_SHSIZE, TPEF::TPEFHeaders::FH_SHSTRTAB, TPEF::TPEFHeaders::FH_SIZE, TPEF::TPEFHeaders::FH_TYPE, TPEF::Section::noBits(), TPEF::ValueReplacer::resolve(), TPEF::Binary::section(), TPEF::Binary::sectionCount(), TPEF::BinaryStream::setTPEFVersion(), TPEF::BinaryStream::setWritePosition(), TPEF::TPEFHeaders::SH_HEADER_SIZE, TPEF::Section::ST_ADDRSP, TPEF::Section::ST_MR, TPEF::Binary::strings(), TPEF::Binary::TPEFVersion(), TPEF::Binary::type(), TPEF::BinaryStream::writeByte(), TPEF::SectionWriter::writeData(), TPEF::BinaryStream::writeHalfWord(), TPEF::SectionWriter::writeHeader(), TPEF::BinaryStream::writePosition(), and TPEF::BinaryStream::writeWord().

Here is the call graph for this function:

◆ instance()

const BinaryWriter & TPEF::TPEFWriter::instance ( )
static

Returns an instance of this class (singleton).

Returns
Instance of class.

Definition at line 70 of file TPEFWriter.cc.

70  {
71  if (instance_ == NULL) {
72  instance_ = new TPEFWriter();
73  }
74  return *instance_;
75 }

References instance_, and TPEFWriter().

Referenced by TPEF::TPEFSectionWriter::parent(), and TTAProgram::Program::writeToTPEF().

Here is the call graph for this function:

Member Data Documentation

◆ instance_

BinaryWriter * TPEF::TPEFWriter::instance_ = NULL
staticprivate

Instance which is used for writing binary file.

Definition at line 58 of file TPEFWriter.hh.

Referenced by instance().


The documentation for this class was generated from the following files:
TPEF::SectionWriter::writeData
static void writeData(BinaryStream &stream, const Section *sect, const BinaryWriter *writer)
Definition: SectionWriter.cc:79
TPEF::BinaryWriter::setWriter
void setWriter(const BinaryWriter *writer)
Definition: BinaryWriter.cc:131
TPEF::TPEFHeaders::FH_TYPE
@ FH_TYPE
Type of TTA program.
Definition: TPEFHeaders.hh:67
TPEF::TPEFHeaders::FH_ID_SIZE
const Byte FH_ID_SIZE
Size of file identification code.
Definition: TPEFHeaders.hh:50
TPEF::BinaryWriter::BinaryWriter
BinaryWriter()
Definition: BinaryWriter.cc:48
TPEF::TPEFHeaders::FH_SHSIZE
@ FH_SHSIZE
Size of section header entry.
Definition: TPEFHeaders.hh:70
TPEF::TPEFHeaders::FH_SHSTRTAB
@ FH_SHSTRTAB
Offset to header of string table.
Definition: TPEFHeaders.hh:72
TPEF::TPEFWriter::TPEFWriter
TPEFWriter()
Definition: TPEFWriter.cc:55
TPEF::TPEFHeaders::FH_ID_BYTES
const Byte FH_ID_BYTES[]
File format identification mark (TPEF version 1). See TPEF documentation for more info.
Definition: TPEFHeaders.hh:45
TPEF::TPEFHeaders::FH_HEADER_SIZE
const HalfWord FH_HEADER_SIZE
Size of file header.
Definition: TPEFHeaders.hh:52
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::TPEFHeaders::TPEFVersion
TPEFVersion
Definition: TPEFHeaders.hh:56
abortWithError
#define abortWithError(message)
Definition: Application.hh:72
TPEF::TPEFWriter::instance_
static BinaryWriter * instance_
Instance which is used for writing binary file.
Definition: TPEFWriter.hh:58
TPEF::TPEFHeaders::FH_SHOFF
@ FH_SHOFF
Offset to first section header.
Definition: TPEFHeaders.hh:68
TPEF::SectionWriter::writeHeader
static void writeHeader(BinaryStream &stream, const Section *sect, const BinaryWriter *writer)
Definition: SectionWriter.cc:62
TPEF::TPEFHeaders::SH_HEADER_SIZE
const HalfWord SH_HEADER_SIZE
Suze of section header.
Definition: TPEFHeaders.hh:54
TPEF::TPEFHeaders::FH_SHNUM
@ FH_SHNUM
Number of section headers.
Definition: TPEFHeaders.hh:71
TPEF::FileOffset
Word FileOffset
Type for storing absolute file offsets.
Definition: TPEFBaseType.hh:52
TPEF::Section::ST_MR
@ ST_MR
Machine resources section.
Definition: Section.hh:78
TPEF::TPEFHeaders::FH_SIZE
@ FH_SIZE
File header size.
Definition: TPEFHeaders.hh:69
TPEF::TPEFHeaders::FH_ID
@ FH_ID
File identification code.
Definition: TPEFHeaders.hh:65
TPEF::TPEFHeaders::FH_ARCH
@ FH_ARCH
Architecture template.
Definition: TPEFHeaders.hh:66
TPEF::Section::ST_ADDRSP
@ ST_ADDRSP
Address space section.
Definition: Section.hh:77
InstanceNotFound
Definition: Exception.hh:304