OpenASIP  2.0
TPEFDataSectionWriter.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file TPEFDataSectionWriter.cc
26  *
27  * Definition of TPEFDataSectionWriter class.
28  *
29  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
30  *
31  * @note rating: yellow
32  */
33 
34 #include <cmath>
35 
36 #include "TPEFDataSectionWriter.hh"
37 #include "DataSection.hh"
38 #include "Chunk.hh"
39 #include "SafePointer.hh"
40 #include "ReferenceKey.hh"
41 #include "TPEFBaseType.hh"
42 #include "Exception.hh"
43 #include "SectionSizeReplacer.hh"
44 #include "ASpaceElement.hh"
45 #include "BinaryStream.hh"
46 
47 namespace TPEF {
48 
49 using ReferenceManager::SafePointer;
50 using ReferenceManager::FileOffsetKey;
51 using ReferenceManager::SectionOffsetKey;
52 using ReferenceManager::SectionKey;
53 
54 const TPEFDataSectionWriter TPEFDataSectionWriter::instance_;
55 
56 /**
57  * Constructor.
58  */
60  if (reg) {
62  }
63 }
64 
65 /**
66  * Destructor.
67  */
69 }
70 
71 /**
72  * Returns section type which for writer is implemented.
73  *
74  * @return Section type which of section that class can write.
75  */
78  return Section::ST_DATA;
79 }
80 
81 /**
82  * Writes section's data into stream.
83  *
84  * @param stream Stream where to write.
85  * @param sect Section to be written.
86  */
87 void
89  BinaryStream& stream,
90  const Section* sect) const {
91 
92  const DataSection* dSection = dynamic_cast<const DataSection*>(sect);
93 
94  assert(dSection != NULL);
95 
96  FileOffset startOffset = stream.writePosition();
97 
98  SectionOffset sectOffset = 0;
99  SectionKey sKey = SafePointer::sectionKeyFor(dSection);
100  SectionId id = sKey.sectionId();
101 
102  for (; sectOffset < dSection->length(); sectOffset++) {
103 
104  Chunk* chunk = NULL;
105 
106  try {
107  chunk = dSection->chunk(sectOffset);
108  } catch (const NotChunkable& n) {
109  bool sectionToBeWrittenIsNotChunkable = false;
110  assert(sectionToBeWrittenIsNotChunkable);
111  }
112 
113  // if referenced or first element of section
114  if (SafePointer::isReferenced(chunk)) {
116  SectionOffsetKey(id, sectOffset), chunk);
117  }
118 
119  stream.writeByte(dSection->byte(chunk));
120  }
121 
122  // needed for writing section header
124  FileOffsetKey(startOffset), dSection->chunk(0));
125 
127  dSection, stream.writePosition() - startOffset);
128 }
129 
130 /**
131  * Returns the fixed size of section elements, in this case smallest
132  * amount of bytes that is needed to store one MAU of address space.
133  *
134  * @param section Section which is written.
135  * @return Size of uninitialized data element in bytes.
136  */
137 Word
139  return static_cast<Word>(
140  ceil(static_cast<double>(section->aSpace()->MAU()) /
141  static_cast<double>(BYTE_BITWIDTH)));
142 }
143 
144 }
TPEF::SectionId
HalfWord SectionId
Type for storing binary file section ids.
Definition: TPEFBaseType.hh:43
TPEF::TPEFDataSectionWriter::TPEFDataSectionWriter
TPEFDataSectionWriter(bool reg=true)
Definition: TPEFDataSectionWriter.cc:59
SectionSizeReplacer.hh
ASpaceElement.hh
TPEF::DataSection::byte
virtual Byte byte(const Chunk *chunk) const
Definition: DataSection.cc:114
TPEF::Section::aSpace
ASpaceElement * aSpace() const
TPEF::ReferenceManager::SafePointer::isReferenced
static bool isReferenced(const SafePointable *object)
Definition: SafePointer.cc:282
Exception.hh
TPEF::TPEFDataSectionWriter::type
virtual Section::SectionType type() const
Definition: TPEFDataSectionWriter.cc:77
TPEF::BinaryStream
Definition: BinaryStream.hh:59
TPEF::BinaryStream::writePosition
unsigned int writePosition()
Definition: BinaryStream.cc:592
SafePointer.hh
TPEF::DataSection
Definition: DataSection.hh:52
TPEF::ASpaceElement::MAU
Byte MAU() const
TPEF::ReferenceManager::SectionKey::sectionId
SectionId sectionId() const
TPEF::ReferenceManager::SafePointer::addObjectReference
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
Definition: SafePointer.cc:306
TPEF::Section
Definition: Section.hh:64
TPEF::TPEFDataSectionWriter::~TPEFDataSectionWriter
virtual ~TPEFDataSectionWriter()
Definition: TPEFDataSectionWriter.cc:68
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::SectionWriter::registerSectionWriter
static void registerSectionWriter(const SectionWriter *sWriter)
Definition: SectionWriter.cc:148
TPEF::Section::ST_DATA
@ ST_DATA
Initialized data section.
Definition: Section.hh:80
TPEF::ReferenceManager::SafePointer::sectionKeyFor
static SectionKey sectionKeyFor(const SafePointable *obj)
Definition: SafePointer.cc:408
TPEF::FileOffset
Word FileOffset
Type for storing absolute file offsets.
Definition: TPEFBaseType.hh:52
DataSection.hh
TPEF::BinaryStream::writeByte
void writeByte(Byte byte)
Definition: BinaryStream.cc:310
TPEF::TPEFDataSectionWriter::elementSize
virtual Word elementSize(const Section *section) const
Definition: TPEFDataSectionWriter.cc:138
TPEF::SectionOffset
Word SectionOffset
Type for storing offsets relative to a given base offset value.
Definition: TPEFBaseType.hh:49
TPEF::ReferenceManager::SectionKey
Definition: ReferenceKey.hh:145
TPEF::ReferenceManager::FileOffsetKey
Definition: ReferenceKey.hh:121
TPEF::SectionSizeReplacer::setSize
static void setSize(const SafePointable *obj, Word size)
Definition: SectionSizeReplacer.cc:100
BYTE_BITWIDTH
const Byte BYTE_BITWIDTH
Definition: BaseType.hh:136
Chunk.hh
TPEF::TPEFDataSectionWriter::actualWriteData
virtual void actualWriteData(BinaryStream &stream, const Section *sect) const
Definition: TPEFDataSectionWriter.cc:88
TPEF::ReferenceManager::SectionOffsetKey
Definition: ReferenceKey.hh:93
BinaryStream.hh
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::DataSection::length
virtual Word length() const
Definition: DataSection.cc:210
NotChunkable
Definition: Exception.hh:353
TPEFBaseType.hh
TPEF::RawSection::chunk
virtual Chunk * chunk(SectionOffset offset) const
Definition: Section.cc:212
ReferenceKey.hh
TPEF::TPEFDataSectionWriter::instance_
static const TPEFDataSectionWriter instance_
Only existing instance of class.
Definition: TPEFDataSectionWriter.hh:68
TPEF::TPEFSectionWriter
Definition: TPEFSectionWriter.hh:49
TPEF::Chunk
Definition: Chunk.hh:45
TPEFDataSectionWriter.hh
TPEF
Definition: Assembler.hh:43