OpenASIP  2.0
TPEFUDataSectionWriter.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 TPEFUDataSectionWriter.cc
26  *
27  * Definition of TPEFUDataSectionWriter class.
28  *
29  * @author Jussi Nykänen 2003 (nykanen-no.spam-cs.tut.fi)
30  * @author Mikael Lepistö 2003 (tmlepist-no.spam-cs.tut.fi)
31  *
32  * @note rating: yellow
33  */
34 
35 #include <cmath>
36 
38 #include "Exception.hh"
39 #include "SectionSizeReplacer.hh"
40 #include "TPEFBaseType.hh"
41 #include "ReferenceKey.hh"
42 #include "SafePointer.hh"
43 #include "UDataSection.hh"
44 #include "BinaryStream.hh"
45 
46 namespace TPEF {
47 
48 using ReferenceManager::SafePointer;
49 using ReferenceManager::FileOffsetKey;
50 using ReferenceManager::SectionOffsetKey;
51 using ReferenceManager::SectionKey;
52 
53 const TPEFUDataSectionWriter TPEFUDataSectionWriter::instance_;
54 
55 /**
56  * Constructor.
57  *
58  * Registers itself to SectionWriter.
59  */
62 }
63 
64 /**
65  * Destructor.
66  */
68 }
69 
70 /**
71  * Returns the type of section, that writer can write.
72  *
73  * @return The type of the section, that writer can write.
74  */
77  return Section::ST_UDATA;
78 }
79 
80 /**
81  * Returns the fixed size of section elements in bytes.
82  *
83  * Size of element depends on address space of section.
84  *
85  * @param section Section which is written.
86  * @return Size of uninitialized data element in bytes.
87  */
88 Word
90  return static_cast<Word>(
91  ceil(static_cast<double>(section->aSpace()->MAU()) /
92  BYTE_BITWIDTH));
93 }
94 
95 /**
96  * Writes section's size into stream.
97  *
98  * @param stream Stream to write.
99  * @param sect Section which size to write.
100  */
101 void
103  const Section* sect) const {
104 
105  const UDataSection* uSect = dynamic_cast<const UDataSection*>(sect);
106  stream.writeWord(uSect->length());
107 }
108 
109 /**
110  * Creates keys to make able to reference UData chunks with section offsets.
111  *
112  * @param sect Section which for keys will be created.
113  */
114 void
116  const UDataSection* uDSection = dynamic_cast<const UDataSection*>(sect);
117 
118  assert(uDSection != NULL);
119 
120  SectionOffset sectOffset = 0;
121  SectionKey sKey = SafePointer::sectionKeyFor(uDSection);
122  SectionId id = sKey.sectionId();
123 
124  // key for first element is always set
125  Chunk* chunk = uDSection->chunk(0);
126 
128  SectionOffsetKey(id, sectOffset), chunk);
129 
130  // go through requested chunks..
131  for (unsigned int i = 0; i < uDSection->referredChunkCount(); i++) {
132  try {
133  chunk = uDSection->referredChunk(i);
134  } catch (const NotChunkable& n) {
135  bool sectionToWriteIsNotChunkable = false;
136  assert(sectionToWriteIsNotChunkable);
137  }
138 
139  sectOffset = chunk->offset();
140 
141  // add section offset to element if really referenced element
142  if (SafePointer::isReferenced(chunk) && sectOffset != 0) {
144  SectionOffsetKey(id, sectOffset), chunk);
145  }
146  }
147 }
148 
149 }
TPEF::TPEFUDataSectionWriter::createKeys
virtual void createKeys(const Section *sect) const
Definition: TPEFUDataSectionWriter.cc:115
TPEF::SectionId
HalfWord SectionId
Type for storing binary file section ids.
Definition: TPEFBaseType.hh:43
SectionSizeReplacer.hh
TPEF::Section::aSpace
ASpaceElement * aSpace() const
TPEF::ReferenceManager::SafePointer::isReferenced
static bool isReferenced(const SafePointable *object)
Definition: SafePointer.cc:282
TPEF::RawSection::referredChunkCount
Word referredChunkCount() const
Definition: Section.cc:366
TPEF::TPEFUDataSectionWriter::type
virtual Section::SectionType type() const
Definition: TPEFUDataSectionWriter.cc:76
Exception.hh
TPEF::TPEFUDataSectionWriter::TPEFUDataSectionWriter
TPEFUDataSectionWriter()
Definition: TPEFUDataSectionWriter.cc:60
TPEF::BinaryStream::writeWord
void writeWord(Word word)
Definition: BinaryStream.cc:368
TPEF::BinaryStream
Definition: BinaryStream.hh:59
SafePointer.hh
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
assert
#define assert(condition)
Definition: Application.hh:86
TPEF::SectionWriter::registerSectionWriter
static void registerSectionWriter(const SectionWriter *sWriter)
Definition: SectionWriter.cc:148
TPEF::RawSection::referredChunk
Chunk * referredChunk(Word index) const
Definition: Section.cc:378
TPEF::ReferenceManager::SafePointer::sectionKeyFor
static SectionKey sectionKeyFor(const SafePointable *obj)
Definition: SafePointer.cc:408
TPEF::UDataSection
Definition: UDataSection.hh:47
TPEF::Section::ST_UDATA
@ ST_UDATA
Uninitialized data section.
Definition: Section.hh:81
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::TPEFUDataSectionWriter::~TPEFUDataSectionWriter
virtual ~TPEFUDataSectionWriter()
Definition: TPEFUDataSectionWriter.cc:67
TPEF::Chunk::offset
SectionOffset offset() const
TPEF::TPEFUDataSectionWriter::elementSize
virtual Word elementSize(const Section *section) const
Definition: TPEFUDataSectionWriter.cc:89
TPEF::TPEFUDataSectionWriter::writeSize
virtual void writeSize(BinaryStream &stream, const Section *sect) const
Definition: TPEFUDataSectionWriter.cc:102
BYTE_BITWIDTH
const Byte BYTE_BITWIDTH
Definition: BaseType.hh:136
UDataSection.hh
TPEF::ReferenceManager::SectionOffsetKey
Definition: ReferenceKey.hh:93
BinaryStream.hh
TPEF::Section::SectionType
SectionType
Definition: Section.hh:69
TPEF::RawSection::length
virtual Word length() const
Definition: Section.cc:275
TPEF::TPEFUDataSectionWriter::instance_
static const TPEFUDataSectionWriter instance_
Unique instance of a class.
Definition: TPEFUDataSectionWriter.hh:70
NotChunkable
Definition: Exception.hh:353
TPEFBaseType.hh
TPEF::RawSection::chunk
virtual Chunk * chunk(SectionOffset offset) const
Definition: Section.cc:212
ReferenceKey.hh
TPEFUDataSectionWriter.hh
TPEF::TPEFSectionWriter
Definition: TPEFSectionWriter.hh:49
TPEF::Chunk
Definition: Chunk.hh:45
TPEF
Definition: Assembler.hh:43