OpenASIP 2.2
Loading...
Searching...
No Matches
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
37#include "DataSection.hh"
38#include "Chunk.hh"
39#include "SafePointer.hh"
40#include "ReferenceKey.hh"
41#include "TPEFBaseType.hh"
42#include "Exception.hh"
44#include "ASpaceElement.hh"
45#include "BinaryStream.hh"
46
47namespace TPEF {
48
49using ReferenceManager::SafePointer;
50using ReferenceManager::FileOffsetKey;
51using ReferenceManager::SectionOffsetKey;
52using ReferenceManager::SectionKey;
53
54const TPEFDataSectionWriter TPEFDataSectionWriter::instance_;
55
56/**
57 * Constructor.
58 */
64
65/**
66 * Destructor.
67 */
70
71/**
72 * Returns section type which for writer is implemented.
73 *
74 * @return Section type which of section that class can write.
75 */
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 */
87void
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;
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 */
137Word
139 return static_cast<Word>(
140 ceil(static_cast<double>(section->aSpace()->MAU()) /
141 static_cast<double>(BYTE_BITWIDTH)));
142}
143
144}
#define assert(condition)
const Byte BYTE_BITWIDTH
Definition BaseType.hh:136
Byte MAU() const
void writeByte(Byte byte)
unsigned int writePosition()
virtual Word length() const
virtual Byte byte(const Chunk *chunk) const
virtual Chunk * chunk(SectionOffset offset) const
Definition Section.cc:212
static void addObjectReference(SectionIndexKey key, const SafePointable *obj)
static SectionKey sectionKeyFor(const SafePointable *obj)
static bool isReferenced(const SafePointable *object)
static void setSize(const SafePointable *obj, Word size)
static void registerSectionWriter(const SectionWriter *sWriter)
@ ST_DATA
Initialized data section.
Definition Section.hh:80
ASpaceElement * aSpace() const
virtual Word elementSize(const Section *section) const
virtual Section::SectionType type() const
virtual void actualWriteData(BinaryStream &stream, const Section *sect) const
static const TPEFDataSectionWriter instance_
Only existing instance of class.
HalfWord SectionId
Type for storing binary file section ids.
Word SectionOffset
Type for storing offsets relative to a given base offset value.
Word FileOffset
Type for storing absolute file offsets.